caches

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2022 License: BSD-3-Clause Imports: 49 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SuffixWebP        = "@GOEDGE_WEBP"    // WebP后缀
	SuffixCompression = "@GOEDGE_"        // 压缩后缀 SuffixCompression + Encoding
	SuffixMethod      = "@GOEDGE_"        // 请求方法后缀 SuffixMethod + RequestMethod
	SuffixPartial     = "@GOEDGE_partial" // 分区缓存后缀
)
View Source
const (
	SizeExpiresAt      = 4
	OffsetExpiresAt    = 0
	SizeStatus         = 3
	OffsetStatus       = SizeExpiresAt
	SizeURLLength      = 4
	OffsetURLLength    = OffsetStatus + SizeStatus
	SizeHeaderLength   = 4
	OffsetHeaderLength = OffsetURLLength + SizeURLLength
	SizeBodyLength     = 8
	OffsetBodyLength   = OffsetHeaderLength + SizeHeaderLength

	SizeMeta  = SizeExpiresAt + SizeStatus + SizeURLLength + SizeHeaderLength + SizeBodyLength
	OffsetKey = SizeMeta
)
View Source
const (
	FileStorageMaxIgnoreKeys       = 32768        // 最大可忽略的键值数(尺寸过大的键值)
	HotItemSize                    = 1024         // 热点数据数量
	HotItemLifeSeconds       int64 = 3600         // 热点数据生命周期
	FileToMemoryMaxSize            = 32 * sizes.M // 可以从文件写入到内存的最大文件尺寸
	FileTmpSuffix                  = ".tmp"
)
View Source
const CountFileDB = 20

Variables

View Source
var (
	ErrNotFound           = errors.New("cache not found")
	ErrFileIsWriting      = errors.New("the file is writing")
	ErrInvalidRange       = errors.New("invalid range")
	ErrEntityTooLarge     = errors.New("entity too large")
	ErrWritingUnavailable = errors.New("writing unavailable")
	ErrWritingQueueFull   = errors.New("writing queue full")
	ErrTooManyOpenFiles   = errors.New("too many open files")
)

常用的几个错误

View Source
var SharedManager = NewManager()

Functions

func CanIgnoreErr

func CanIgnoreErr(err error) bool

CanIgnoreErr 检查错误是否可以忽略

func NewCapacityError

func NewCapacityError(err string) error

Types

type CapacityError

type CapacityError struct {
	// contains filtered or unexported fields
}

CapacityError 容量错误 独立出来是为了可以在有些场合下可以忽略,防止产生没必要的错误提示数量太多

func (*CapacityError) Error

func (this *CapacityError) Error() string

type FileList

type FileList struct {
	// contains filtered or unexported fields
}

FileList 文件缓存列表管理

func (*FileList) Add

func (this *FileList) Add(hash string, item *Item) error

func (*FileList) CleanAll

func (this *FileList) CleanAll() error

func (*FileList) CleanPrefix

func (this *FileList) CleanPrefix(prefix string) error

CleanPrefix 清理某个前缀的缓存数据

func (*FileList) Close

func (this *FileList) Close() error

func (*FileList) Count

func (this *FileList) Count() (int64, error)

Count 总数量 常用的方法,所以避免直接查询数据库

func (*FileList) Exist

func (this *FileList) Exist(hash string) (bool, error)

func (*FileList) GetDBIndex

func (this *FileList) GetDBIndex(hash string) uint64

func (*FileList) IncreaseHit

func (this *FileList) IncreaseHit(hash string) error

IncreaseHit 增加点击量

func (*FileList) Init

func (this *FileList) Init() error

func (*FileList) OnAdd

func (this *FileList) OnAdd(f func(item *Item))

OnAdd 添加事件

func (*FileList) OnRemove

func (this *FileList) OnRemove(f func(item *Item))

OnRemove 删除事件

func (*FileList) Purge

func (this *FileList) Purge(count int, callback func(hash string) error) (int, error)

Purge 清理过期的缓存 count 每次遍历的最大数量,控制此数字可以保证每次清理的时候不用花太多时间 callback 每次发现过期key的调用

func (*FileList) PurgeLFU

func (this *FileList) PurgeLFU(count int, callback func(hash string) error) error

func (*FileList) Remove

func (this *FileList) Remove(hash string) error

func (*FileList) Reset

func (this *FileList) Reset() error

func (*FileList) SetOldDir

func (this *FileList) SetOldDir(oldDir string)

func (*FileList) Stat

func (this *FileList) Stat(check func(hash string) bool) (*Stat, error)

func (*FileList) UpgradeV3

func (this *FileList) UpgradeV3(oldDir string, brokenOnError bool) error

type FileListDB

type FileListDB struct {
	// contains filtered or unexported fields
}

func NewFileListDB

func NewFileListDB() *FileListDB

func (*FileListDB) Add

func (this *FileListDB) Add(hash string, item *Item) error

func (*FileListDB) CleanAll

func (this *FileListDB) CleanAll() error

func (*FileListDB) CleanPrefix

func (this *FileListDB) CleanPrefix(prefix string) error

func (*FileListDB) Close

func (this *FileListDB) Close() error

func (*FileListDB) IncreaseHit

func (this *FileListDB) IncreaseHit(hash string) error

func (*FileListDB) Init

func (this *FileListDB) Init() error

func (*FileListDB) IsReady

func (this *FileListDB) IsReady() bool

func (*FileListDB) ListExpiredItems

func (this *FileListDB) ListExpiredItems(count int) (hashList []string, err error)

func (*FileListDB) ListLFUItems

func (this *FileListDB) ListLFUItems(count int) (hashList []string, err error)

func (*FileListDB) Open

func (this *FileListDB) Open(dbPath string) error

func (*FileListDB) Total

func (this *FileListDB) Total() int64

func (*FileListDB) WrapError added in v0.5.0

func (this *FileListDB) WrapError(err error) error

type FileReader

type FileReader struct {
	// contains filtered or unexported fields
}

func NewFileReader

func NewFileReader(fp *os.File) *FileReader

func (*FileReader) BodySize

func (this *FileReader) BodySize() int64

func (*FileReader) Close

func (this *FileReader) Close() error

func (*FileReader) ContainsRange

func (this *FileReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)

ContainsRange 是否包含某些区间内容

func (*FileReader) ExpiresAt

func (this *FileReader) ExpiresAt() int64

func (*FileReader) FP

func (this *FileReader) FP() *os.File

FP 原始的文件句柄

func (*FileReader) HeaderSize

func (this *FileReader) HeaderSize() int64

func (*FileReader) Init

func (this *FileReader) Init() error

func (*FileReader) InitAutoDiscard

func (this *FileReader) InitAutoDiscard(autoDiscard bool) error

func (*FileReader) LastModified

func (this *FileReader) LastModified() int64

func (*FileReader) Read

func (this *FileReader) Read(buf []byte) (n int, err error)

func (*FileReader) ReadBody

func (this *FileReader) ReadBody(buf []byte, callback ReaderFunc) error

func (*FileReader) ReadBodyRange

func (this *FileReader) ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error

func (*FileReader) ReadHeader

func (this *FileReader) ReadHeader(buf []byte, callback ReaderFunc) error

func (*FileReader) Status

func (this *FileReader) Status() int

func (*FileReader) TypeName

func (this *FileReader) TypeName() string

type FileStorage

type FileStorage struct {
	// contains filtered or unexported fields
}

FileStorage 文件缓存

文件结构:
 [expires time] | [ status ] | [url length] | [header length] | [body length] | [url] [header data] [body data]

func NewFileStorage

func NewFileStorage(policy *serverconfigs.HTTPCachePolicy) *FileStorage

func (*FileStorage) AddToList

func (this *FileStorage) AddToList(item *Item)

AddToList 添加到List

func (*FileStorage) CanSendfile

func (this *FileStorage) CanSendfile() bool

CanSendfile 是否支持Sendfile

func (*FileStorage) CanUpdatePolicy

func (this *FileStorage) CanUpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy) bool

CanUpdatePolicy 检查策略是否可以更新

func (*FileStorage) CleanAll

func (this *FileStorage) CleanAll() error

CleanAll 清除所有的缓存

func (*FileStorage) Delete

func (this *FileStorage) Delete(key string) error

Delete 删除某个键值对应的缓存

func (*FileStorage) IgnoreKey

func (this *FileStorage) IgnoreKey(key string)

IgnoreKey 忽略某个Key,即不缓存某个Key

func (*FileStorage) Init

func (this *FileStorage) Init() error

Init 初始化

func (*FileStorage) OpenFlushWriter

func (this *FileStorage) OpenFlushWriter(key string, expiresAt int64, status int) (Writer, error)

OpenFlushWriter 打开从其他媒介直接刷入的写入器

func (*FileStorage) OpenReader

func (this *FileStorage) OpenReader(key string, useStale bool, isPartial bool) (Reader, error)

func (*FileStorage) OpenWriter

func (this *FileStorage) OpenWriter(key string, expiresAt int64, status int, size int64, maxSize int64, isPartial bool) (Writer, error)

OpenWriter 打开缓存文件等待写入

func (*FileStorage) Policy

func (this *FileStorage) Policy() *serverconfigs.HTTPCachePolicy

Policy 获取当前的Policy

func (*FileStorage) Purge

func (this *FileStorage) Purge(keys []string, urlType string) error

Purge 清理过期的缓存

func (*FileStorage) Stat

func (this *FileStorage) Stat() (*Stat, error)

Stat 统计

func (*FileStorage) Stop

func (this *FileStorage) Stop()

Stop 停止

func (*FileStorage) TotalDiskSize

func (this *FileStorage) TotalDiskSize() int64

TotalDiskSize 消耗的磁盘尺寸

func (*FileStorage) TotalMemorySize

func (this *FileStorage) TotalMemorySize() int64

TotalMemorySize 内存尺寸

func (*FileStorage) UpdatePolicy

func (this *FileStorage) UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy)

UpdatePolicy 修改策略

type FileWriter

type FileWriter struct {
	// contains filtered or unexported fields
}

func NewFileWriter

func NewFileWriter(storage StorageInterface, rawWriter *os.File, key string, expiredAt int64, maxSize int64, endFunc func()) *FileWriter

func (*FileWriter) BodySize

func (this *FileWriter) BodySize() int64

func (*FileWriter) Close

func (this *FileWriter) Close() error

Close 关闭

func (*FileWriter) Discard

func (this *FileWriter) Discard() error

Discard 丢弃

func (*FileWriter) ExpiredAt

func (this *FileWriter) ExpiredAt() int64

func (*FileWriter) HeaderSize

func (this *FileWriter) HeaderSize() int64

func (*FileWriter) ItemType

func (this *FileWriter) ItemType() ItemType

ItemType 获取内容类型

func (*FileWriter) Key

func (this *FileWriter) Key() string

func (*FileWriter) Write

func (this *FileWriter) Write(data []byte) (n int, err error)

Write 写入数据

func (*FileWriter) WriteAt

func (this *FileWriter) WriteAt(offset int64, data []byte) error

WriteAt 在指定位置写入数据

func (*FileWriter) WriteBodyLength

func (this *FileWriter) WriteBodyLength(bodyLength int64) error

WriteBodyLength 写入Body长度数据

func (*FileWriter) WriteHeader

func (this *FileWriter) WriteHeader(data []byte) (n int, err error)

WriteHeader 写入数据

func (*FileWriter) WriteHeaderLength

func (this *FileWriter) WriteHeaderLength(headerLength int) error

WriteHeaderLength 写入Header长度数据

type HotItem

type HotItem struct {
	Key  string
	Hits uint32
}

type Item

type Item struct {
	Type       ItemType `json:"type"`
	Key        string   `json:"key"`
	ExpiredAt  int64    `json:"expiredAt"`
	StaleAt    int64    `json:"staleAt"`
	HeaderSize int64    `json:"headerSize"`
	BodySize   int64    `json:"bodySize"`
	MetaSize   int64    `json:"metaSize"`
	Host       string   `json:"host"`     // 主机名
	ServerId   int64    `json:"serverId"` // 服务ID

	Week1Hits int64 `json:"week1Hits"`
	Week2Hits int64 `json:"week2Hits"`
	Week      int32 `json:"week"`
}

func (*Item) IncreaseHit

func (this *Item) IncreaseHit(week int32)

func (*Item) IsExpired

func (this *Item) IsExpired() bool

func (*Item) Size

func (this *Item) Size() int64

func (*Item) TotalSize

func (this *Item) TotalSize() int64

type ItemType

type ItemType = int
const (
	ItemTypeFile   ItemType = 1
	ItemTypeMemory ItemType = 2
)

type ListInterface

type ListInterface interface {
	// Init 初始化
	Init() error

	// Reset 重置数据
	Reset() error

	// Add 添加内容
	Add(hash string, item *Item) error

	// Exist 检查内容是否存在
	Exist(hash string) (bool, error)

	// CleanPrefix 清除某个前缀的缓存
	CleanPrefix(prefix string) error

	// Remove 删除内容
	Remove(hash string) error

	// Purge 清理过期数据
	Purge(count int, callback func(hash string) error) (int, error)

	// PurgeLFU 清理LFU数据
	PurgeLFU(count int, callback func(hash string) error) error

	// CleanAll 清除所有缓存
	CleanAll() error

	// Stat 统计
	Stat(check func(hash string) bool) (*Stat, error)

	// Count 总数量
	Count() (int64, error)

	// OnAdd 添加事件
	OnAdd(f func(item *Item))

	// OnRemove 删除事件
	OnRemove(f func(item *Item))

	// Close 关闭
	Close() error

	// IncreaseHit 增加点击量
	IncreaseHit(hash string) error
}

func NewFileList

func NewFileList(dir string) ListInterface

func NewMemoryList

func NewMemoryList() ListInterface

type Manager

type Manager struct {
	// 全局配置
	MaxDiskCapacity   *shared.SizeCapacity
	DiskDir           string
	MaxMemoryCapacity *shared.SizeCapacity
	// contains filtered or unexported fields
}

Manager 缓存策略管理器

func NewManager

func NewManager() *Manager

NewManager 获取管理器对象

func (*Manager) FindAllCachePaths

func (this *Manager) FindAllCachePaths() []string

FindAllCachePaths 所有缓存路径

func (*Manager) FindAllStorages

func (this *Manager) FindAllStorages() []StorageInterface

FindAllStorages 读取所有缓存存储

func (*Manager) FindPolicy

func (this *Manager) FindPolicy(policyId int64) *serverconfigs.HTTPCachePolicy

FindPolicy 获取Policy信息

func (*Manager) FindStorageWithPolicy

func (this *Manager) FindStorageWithPolicy(policyId int64) StorageInterface

FindStorageWithPolicy 根据策略ID查找存储

func (*Manager) NewStorageWithPolicy

func (this *Manager) NewStorageWithPolicy(policy *serverconfigs.HTTPCachePolicy) StorageInterface

NewStorageWithPolicy 根据策略获取存储对象

func (*Manager) TotalDiskSize

func (this *Manager) TotalDiskSize() int64

TotalDiskSize 消耗的磁盘尺寸

func (*Manager) TotalMemorySize

func (this *Manager) TotalMemorySize() int64

TotalMemorySize 消耗的内存尺寸

func (*Manager) UpdatePolicies

func (this *Manager) UpdatePolicies(newPolicies []*serverconfigs.HTTPCachePolicy)

UpdatePolicies 重新设置策略

type MaxOpenFiles

type MaxOpenFiles struct {
	// contains filtered or unexported fields
}

MaxOpenFiles max open files manager

func NewMaxOpenFiles

func NewMaxOpenFiles() *MaxOpenFiles

func (*MaxOpenFiles) Fast

func (this *MaxOpenFiles) Fast()

func (*MaxOpenFiles) FinishAll added in v0.4.9

func (this *MaxOpenFiles) FinishAll()

func (*MaxOpenFiles) Next added in v0.4.9

func (this *MaxOpenFiles) Next() bool

func (*MaxOpenFiles) Slow

func (this *MaxOpenFiles) Slow()

type MemoryItem

type MemoryItem struct {
	ExpiresAt   int64
	HeaderValue []byte
	BodyValue   []byte
	Status      int
	IsDone      bool
	ModifiedAt  int64
}

func (*MemoryItem) IsExpired

func (this *MemoryItem) IsExpired() bool

type MemoryList

type MemoryList struct {
	// contains filtered or unexported fields
}

MemoryList 内存缓存列表管理

func (*MemoryList) Add

func (this *MemoryList) Add(hash string, item *Item) error

func (*MemoryList) CleanAll

func (this *MemoryList) CleanAll() error

func (*MemoryList) CleanPrefix

func (this *MemoryList) CleanPrefix(prefix string) error

CleanPrefix 根据前缀进行清除

func (*MemoryList) Close

func (this *MemoryList) Close() error

func (*MemoryList) Count

func (this *MemoryList) Count() (int64, error)

Count 总数量

func (*MemoryList) Exist

func (this *MemoryList) Exist(hash string) (bool, error)

func (*MemoryList) IncreaseHit

func (this *MemoryList) IncreaseHit(hash string) error

IncreaseHit 增加点击量

func (*MemoryList) Init

func (this *MemoryList) Init() error

func (*MemoryList) OnAdd

func (this *MemoryList) OnAdd(f func(item *Item))

OnAdd 添加事件

func (*MemoryList) OnRemove

func (this *MemoryList) OnRemove(f func(item *Item))

OnRemove 删除事件

func (*MemoryList) Purge

func (this *MemoryList) Purge(count int, callback func(hash string) error) (int, error)

Purge 清理过期的缓存 count 每次遍历的最大数量,控制此数字可以保证每次清理的时候不用花太多时间 callback 每次发现过期key的调用

func (*MemoryList) PurgeLFU

func (this *MemoryList) PurgeLFU(count int, callback func(hash string) error) error

func (*MemoryList) Remove

func (this *MemoryList) Remove(hash string) error

func (*MemoryList) Reset

func (this *MemoryList) Reset() error

func (*MemoryList) Stat

func (this *MemoryList) Stat(check func(hash string) bool) (*Stat, error)

type MemoryReader

type MemoryReader struct {
	// contains filtered or unexported fields
}

func NewMemoryReader

func NewMemoryReader(item *MemoryItem) *MemoryReader

func (*MemoryReader) BodySize

func (this *MemoryReader) BodySize() int64

func (*MemoryReader) Close

func (this *MemoryReader) Close() error

func (*MemoryReader) ContainsRange

func (this *MemoryReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)

ContainsRange 是否包含某些区间内容

func (*MemoryReader) ExpiresAt

func (this *MemoryReader) ExpiresAt() int64

func (*MemoryReader) HeaderSize

func (this *MemoryReader) HeaderSize() int64

func (*MemoryReader) Init

func (this *MemoryReader) Init() error

func (*MemoryReader) LastModified

func (this *MemoryReader) LastModified() int64

func (*MemoryReader) Read

func (this *MemoryReader) Read(buf []byte) (n int, err error)

func (*MemoryReader) ReadBody

func (this *MemoryReader) ReadBody(buf []byte, callback ReaderFunc) error

func (*MemoryReader) ReadBodyRange

func (this *MemoryReader) ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error

func (*MemoryReader) ReadHeader

func (this *MemoryReader) ReadHeader(buf []byte, callback ReaderFunc) error

func (*MemoryReader) Status

func (this *MemoryReader) Status() int

func (*MemoryReader) TypeName

func (this *MemoryReader) TypeName() string

type MemoryStorage

type MemoryStorage struct {
	// contains filtered or unexported fields
}

func NewMemoryStorage

func NewMemoryStorage(policy *serverconfigs.HTTPCachePolicy, parentStorage StorageInterface) *MemoryStorage

func (*MemoryStorage) AddToList

func (this *MemoryStorage) AddToList(item *Item)

AddToList 将缓存添加到列表

func (*MemoryStorage) CanSendfile

func (this *MemoryStorage) CanSendfile() bool

CanSendfile 是否支持Sendfile

func (*MemoryStorage) CanUpdatePolicy

func (this *MemoryStorage) CanUpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy) bool

CanUpdatePolicy 检查策略是否可以更新

func (*MemoryStorage) CleanAll

func (this *MemoryStorage) CleanAll() error

CleanAll 清除所有缓存

func (*MemoryStorage) Delete

func (this *MemoryStorage) Delete(key string) error

Delete 删除某个键值对应的缓存

func (*MemoryStorage) IgnoreKey

func (this *MemoryStorage) IgnoreKey(key string)

IgnoreKey 忽略某个Key,即不缓存某个Key

func (*MemoryStorage) Init

func (this *MemoryStorage) Init() error

Init 初始化

func (*MemoryStorage) OpenFlushWriter

func (this *MemoryStorage) OpenFlushWriter(key string, expiresAt int64, status int) (Writer, error)

OpenFlushWriter 打开从其他媒介直接刷入的写入器

func (*MemoryStorage) OpenReader

func (this *MemoryStorage) OpenReader(key string, useStale bool, isPartial bool) (Reader, error)

OpenReader 读取缓存

func (*MemoryStorage) OpenWriter

func (this *MemoryStorage) OpenWriter(key string, expiredAt int64, status int, size int64, maxSize int64, isPartial bool) (Writer, error)

OpenWriter 打开缓存写入器等待写入

func (*MemoryStorage) Policy

Policy 获取当前存储的Policy

func (*MemoryStorage) Purge

func (this *MemoryStorage) Purge(keys []string, urlType string) error

Purge 批量删除缓存

func (*MemoryStorage) Stat

func (this *MemoryStorage) Stat() (*Stat, error)

Stat 统计缓存

func (*MemoryStorage) Stop

func (this *MemoryStorage) Stop()

Stop 停止缓存策略

func (*MemoryStorage) TotalDiskSize

func (this *MemoryStorage) TotalDiskSize() int64

TotalDiskSize 消耗的磁盘尺寸

func (*MemoryStorage) TotalMemorySize

func (this *MemoryStorage) TotalMemorySize() int64

TotalMemorySize 内存尺寸

func (*MemoryStorage) UpdatePolicy

func (this *MemoryStorage) UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy)

UpdatePolicy 修改策略

type MemoryWriter

type MemoryWriter struct {
	// contains filtered or unexported fields
}

func NewMemoryWriter

func NewMemoryWriter(memoryStorage *MemoryStorage, key string, expiredAt int64, status int, isDirty bool, maxSize int64, endFunc func()) *MemoryWriter

func (*MemoryWriter) BodySize

func (this *MemoryWriter) BodySize() int64

BodySize 主体内容尺寸

func (*MemoryWriter) Close

func (this *MemoryWriter) Close() error

Close 关闭

func (*MemoryWriter) Discard

func (this *MemoryWriter) Discard() error

Discard 丢弃

func (*MemoryWriter) ExpiredAt

func (this *MemoryWriter) ExpiredAt() int64

ExpiredAt 过期时间

func (*MemoryWriter) HeaderSize

func (this *MemoryWriter) HeaderSize() int64

HeaderSize 数据尺寸

func (*MemoryWriter) ItemType

func (this *MemoryWriter) ItemType() ItemType

ItemType 内容类型

func (*MemoryWriter) Key

func (this *MemoryWriter) Key() string

Key 获取Key

func (*MemoryWriter) Write

func (this *MemoryWriter) Write(data []byte) (n int, err error)

Write 写入数据

func (*MemoryWriter) WriteAt

func (this *MemoryWriter) WriteAt(offset int64, b []byte) error

WriteAt 在指定位置写入数据

func (*MemoryWriter) WriteHeader

func (this *MemoryWriter) WriteHeader(data []byte) (n int, err error)

WriteHeader 写入数据

type OpenFile

type OpenFile struct {
	// contains filtered or unexported fields
}

func NewOpenFile

func NewOpenFile(fp *os.File, meta []byte, header []byte, version int64) *OpenFile

func (*OpenFile) Close

func (this *OpenFile) Close() error

func (*OpenFile) SeekStart

func (this *OpenFile) SeekStart() error

type OpenFileCache

type OpenFileCache struct {
	// contains filtered or unexported fields
}

func NewOpenFileCache

func NewOpenFileCache(maxSize int) (*OpenFileCache, error)

func (*OpenFileCache) Close

func (this *OpenFileCache) Close(filename string)

func (*OpenFileCache) CloseAll

func (this *OpenFileCache) CloseAll()

func (*OpenFileCache) Debug

func (this *OpenFileCache) Debug()

func (*OpenFileCache) Get

func (this *OpenFileCache) Get(filename string) *OpenFile

func (*OpenFileCache) Put

func (this *OpenFileCache) Put(filename string, file *OpenFile)

type OpenFilePool

type OpenFilePool struct {
	// contains filtered or unexported fields
}

func NewOpenFilePool

func NewOpenFilePool(filename string) *OpenFilePool

func (*OpenFilePool) Close

func (this *OpenFilePool) Close()

func (*OpenFilePool) Filename

func (this *OpenFilePool) Filename() string

func (*OpenFilePool) Get

func (this *OpenFilePool) Get() (*OpenFile, bool)

func (*OpenFilePool) Len

func (this *OpenFilePool) Len() int

func (*OpenFilePool) Put

func (this *OpenFilePool) Put(file *OpenFile) bool

type PartialFileReader

type PartialFileReader struct {
	*FileReader
	// contains filtered or unexported fields
}

func NewPartialFileReader

func NewPartialFileReader(fp *os.File) *PartialFileReader

func (*PartialFileReader) ContainsRange

func (this *PartialFileReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)

ContainsRange 是否包含某些区间内容 这里的 r 是已经经过格式化的

func (*PartialFileReader) Init

func (this *PartialFileReader) Init() error

func (*PartialFileReader) InitAutoDiscard

func (this *PartialFileReader) InitAutoDiscard(autoDiscard bool) error

func (*PartialFileReader) MaxLength

func (this *PartialFileReader) MaxLength() int64

MaxLength 获取区间最大长度

type PartialFileWriter

type PartialFileWriter struct {
	// contains filtered or unexported fields
}

func NewPartialFileWriter

func NewPartialFileWriter(rawWriter *os.File, key string, expiredAt int64, isNew bool, isPartial bool, bodyOffset int64, ranges *PartialRanges, endFunc func()) *PartialFileWriter

func (*PartialFileWriter) AppendHeader

func (this *PartialFileWriter) AppendHeader(data []byte) error

func (*PartialFileWriter) BodySize

func (this *PartialFileWriter) BodySize() int64

func (*PartialFileWriter) Close

func (this *PartialFileWriter) Close() error

Close 关闭

func (*PartialFileWriter) Discard

func (this *PartialFileWriter) Discard() error

Discard 丢弃

func (*PartialFileWriter) ExpiredAt

func (this *PartialFileWriter) ExpiredAt() int64

func (*PartialFileWriter) HeaderSize

func (this *PartialFileWriter) HeaderSize() int64

func (*PartialFileWriter) IsNew

func (this *PartialFileWriter) IsNew() bool

func (*PartialFileWriter) ItemType

func (this *PartialFileWriter) ItemType() ItemType

ItemType 获取内容类型

func (*PartialFileWriter) Key

func (this *PartialFileWriter) Key() string

func (*PartialFileWriter) SetBodyLength

func (this *PartialFileWriter) SetBodyLength(bodyLength int64)

SetBodyLength 设置内容总长度

func (*PartialFileWriter) Write

func (this *PartialFileWriter) Write(data []byte) (n int, err error)

Write 写入数据

func (*PartialFileWriter) WriteAt

func (this *PartialFileWriter) WriteAt(offset int64, data []byte) error

WriteAt 在指定位置写入数据

func (*PartialFileWriter) WriteBodyLength

func (this *PartialFileWriter) WriteBodyLength(bodyLength int64) error

WriteBodyLength 写入Body长度数据

func (*PartialFileWriter) WriteHeader

func (this *PartialFileWriter) WriteHeader(data []byte) (n int, err error)

WriteHeader 写入数据

func (*PartialFileWriter) WriteHeaderLength

func (this *PartialFileWriter) WriteHeaderLength(headerLength int) error

WriteHeaderLength 写入Header长度数据

type PartialRanges

type PartialRanges struct {
	Ranges [][2]int64 `json:"ranges"`
}

PartialRanges 内容分区范围定义

func NewPartialRanges

func NewPartialRanges() *PartialRanges

NewPartialRanges 获取新对象

func NewPartialRangesFromFile

func NewPartialRangesFromFile(path string) (*PartialRanges, error)

func NewPartialRangesFromJSON

func NewPartialRangesFromJSON(data []byte) (*PartialRanges, error)

NewPartialRangesFromJSON 从JSON中解析范围

func (*PartialRanges) Add

func (this *PartialRanges) Add(begin int64, end int64)

Add 添加新范围

func (*PartialRanges) AsJSON

func (this *PartialRanges) AsJSON() ([]byte, error)

AsJSON 转换为JSON

func (*PartialRanges) Contains

func (this *PartialRanges) Contains(begin int64, end int64) bool

Contains 检查是否包含某个范围

func (*PartialRanges) Max

func (this *PartialRanges) Max() int64

func (*PartialRanges) Nearest

func (this *PartialRanges) Nearest(begin int64, end int64) (r [2]int64, ok bool)

Nearest 查找最近的某个范围

func (*PartialRanges) ReadFromFile

func (this *PartialRanges) ReadFromFile(path string) (*PartialRanges, error)

ReadFromFile 从文件中读取

func (*PartialRanges) WriteToFile

func (this *PartialRanges) WriteToFile(path string) error

WriteToFile 写入到文件中

type Reader

type Reader interface {
	// Init 初始化
	Init() error

	// TypeName 类型名称
	TypeName() string

	// ExpiresAt 过期时间
	ExpiresAt() int64

	// Status 状态码
	Status() int

	// LastModified 最后修改时间
	LastModified() int64

	// ReadHeader 读取Header
	ReadHeader(buf []byte, callback ReaderFunc) error

	// ReadBody 读取Body
	ReadBody(buf []byte, callback ReaderFunc) error

	// Read 实现io.Reader接口
	Read(buf []byte) (int, error)

	// ReadBodyRange 读取某个范围内的Body
	ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error

	// HeaderSize Header Size
	HeaderSize() int64

	// BodySize Body Size
	BodySize() int64

	// ContainsRange 是否包含某个区间内容
	ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)

	// Close 关闭
	Close() error
}

type ReaderFunc

type ReaderFunc func(n int) (goNext bool, err error)

type Stat

type Stat struct {
	Count     int   // 数量
	ValueSize int64 // 值占用的空间
	Size      int64 // 占用的空间尺寸
}

type StorageInterface

type StorageInterface interface {
	// Init 初始化
	Init() error

	// OpenReader 读取缓存
	OpenReader(key string, useStale bool, isPartial bool) (reader Reader, err error)

	// OpenWriter 打开缓存写入器等待写入
	// size 和 maxSize 可能为-1
	OpenWriter(key string, expiresAt int64, status int, size int64, maxSize int64, isPartial bool) (Writer, error)

	// OpenFlushWriter 打开从其他媒介直接刷入的写入器
	OpenFlushWriter(key string, expiresAt int64, status int) (Writer, error)

	// Delete 删除某个键值对应的缓存
	Delete(key string) error

	// Stat 统计缓存
	Stat() (*Stat, error)

	// TotalDiskSize 消耗的磁盘尺寸
	TotalDiskSize() int64

	// TotalMemorySize 内存尺寸
	TotalMemorySize() int64

	// CleanAll 清除所有缓存
	CleanAll() error

	// Purge 批量删除缓存
	// urlType 值为file|dir
	Purge(keys []string, urlType string) error

	// Stop 停止缓存策略
	Stop()

	// Policy 获取当前存储的Policy
	Policy() *serverconfigs.HTTPCachePolicy

	// UpdatePolicy 修改策略
	UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy)

	// CanUpdatePolicy 检查策略是否可以更新
	CanUpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy) bool

	// AddToList 将缓存添加到列表
	AddToList(item *Item)

	// IgnoreKey 忽略某个Key,即不缓存某个Key
	IgnoreKey(key string)

	// CanSendfile 是否支持Sendfile
	CanSendfile() bool
}

StorageInterface 缓存存储接口

type Writer

type Writer interface {
	// WriteHeader 写入Header数据
	WriteHeader(data []byte) (n int, err error)

	// Write 写入Body数据
	Write(data []byte) (n int, err error)

	// WriteAt 在指定位置写入数据
	WriteAt(offset int64, data []byte) error

	// HeaderSize 写入的Header数据大小
	HeaderSize() int64

	// BodySize 写入的Body数据大小
	BodySize() int64

	// Close 关闭
	Close() error

	// Discard 丢弃
	Discard() error

	// Key Key
	Key() string

	// ExpiredAt 过期时间
	ExpiredAt() int64

	// ItemType 内容类型
	ItemType() ItemType
}

Writer 缓存内容写入接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL