Documentation
¶
Index ¶
- Constants
- Variables
- func CanIgnoreErr(err error) bool
- func NewCapacityError(err string) error
- func ParseHost(key string) string
- type CapacityError
- type FileDir
- type FileList
- func (this *FileList) Add(hash string, item *Item) error
- func (this *FileList) CleanAll() error
- func (this *FileList) CleanMatchKey(key string) error
- func (this *FileList) CleanMatchPrefix(prefix string) error
- func (this *FileList) CleanPrefix(prefix string) error
- func (this *FileList) Close() error
- func (this *FileList) Count() (int64, error)
- func (this *FileList) Exist(hash string) (bool, error)
- func (this *FileList) GetDB(hash string) *FileListDB
- func (this *FileList) GetDBIndex(hash string) uint64
- func (this *FileList) IncreaseHit(hash string) error
- func (this *FileList) Init() error
- func (this *FileList) OnAdd(f func(item *Item))
- func (this *FileList) OnRemove(f func(item *Item))
- func (this *FileList) Purge(count int, callback func(hash string) error) (int, error)
- func (this *FileList) PurgeLFU(count int, callback func(hash string) error) error
- func (this *FileList) Remove(hash string) error
- func (this *FileList) Reset() error
- func (this *FileList) SetOldDir(oldDir string)
- func (this *FileList) Stat(check func(hash string) bool) (*Stat, error)
- func (this *FileList) UpgradeV3(oldDir string, brokenOnError bool) error
- type FileListDB
- func (this *FileListDB) AddSync(hash string, item *Item) error
- func (this *FileListDB) CleanAll() error
- func (this *FileListDB) CleanMatchKey(key string) error
- func (this *FileListDB) CleanMatchPrefix(prefix string) error
- func (this *FileListDB) CleanPrefix(prefix string) error
- func (this *FileListDB) Close() error
- func (this *FileListDB) DeleteHitAsync(hash string) error
- func (this *FileListDB) DeleteSync(hash string) error
- func (this *FileListDB) IncreaseHitAsync(hash string) error
- func (this *FileListDB) Init() error
- func (this *FileListDB) IsReady() bool
- func (this *FileListDB) ListExpiredItems(count int) (hashList []string, err error)
- func (this *FileListDB) ListHashes(lastId int64) (hashList []string, maxId int64, err error)
- func (this *FileListDB) ListLFUItems(count int) (hashList []string, err error)
- func (this *FileListDB) Open(dbPath string) error
- func (this *FileListDB) Total() (int64, error)
- func (this *FileListDB) WrapError(err error) error
- type FileListHashMap
- func (this *FileListHashMap) Add(hash string)
- func (this *FileListHashMap) AddHashes(hashes []string)
- func (this *FileListHashMap) Clean()
- func (this *FileListHashMap) Delete(hash string)
- func (this *FileListHashMap) Exist(hash string) bool
- func (this *FileListHashMap) IsReady() bool
- func (this *FileListHashMap) Len() int
- func (this *FileListHashMap) Load(db *FileListDB) error
- type FileReader
- func (this *FileReader) BodySize() int64
- func (this *FileReader) Close() error
- func (this *FileReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)
- func (this *FileReader) ExpiresAt() int64
- func (this *FileReader) FP() *os.File
- func (this *FileReader) HeaderSize() int64
- func (this *FileReader) Init() error
- func (this *FileReader) InitAutoDiscard(autoDiscard bool) error
- func (this *FileReader) LastModified() int64
- func (this *FileReader) Read(buf []byte) (n int, err error)
- func (this *FileReader) ReadBody(buf []byte, callback ReaderFunc) error
- func (this *FileReader) ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error
- func (this *FileReader) ReadHeader(buf []byte, callback ReaderFunc) error
- func (this *FileReader) Status() int
- func (this *FileReader) TypeName() string
- type FileStorage
- func (this *FileStorage) AddToList(item *Item)
- func (this *FileStorage) CanSendfile() bool
- func (this *FileStorage) CanUpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy) bool
- func (this *FileStorage) CleanAll() error
- func (this *FileStorage) Delete(key string) error
- func (this *FileStorage) IgnoreKey(key string, maxSize int64)
- func (this *FileStorage) Init() error
- func (this *FileStorage) OpenFlushWriter(key string, expiresAt int64, status int, headerSize int, bodySize int64) (Writer, error)
- func (this *FileStorage) OpenReader(key string, useStale bool, isPartial bool) (Reader, error)
- func (this *FileStorage) OpenWriter(key string, expiresAt int64, status int, headerSize int, bodySize int64, ...) (Writer, error)
- func (this *FileStorage) Policy() *serverconfigs.HTTPCachePolicy
- func (this *FileStorage) Purge(keys []string, urlType string) error
- func (this *FileStorage) Stat() (*Stat, error)
- func (this *FileStorage) Stop()
- func (this *FileStorage) TotalDiskSize() int64
- func (this *FileStorage) TotalMemorySize() int64
- func (this *FileStorage) UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy)
- type FileWriter
- func (this *FileWriter) BodySize() int64
- func (this *FileWriter) Close() error
- func (this *FileWriter) Discard() error
- func (this *FileWriter) ExpiredAt() int64
- func (this *FileWriter) HeaderSize() int64
- func (this *FileWriter) ItemType() ItemType
- func (this *FileWriter) Key() string
- func (this *FileWriter) Write(data []byte) (n int, err error)
- func (this *FileWriter) WriteAt(offset int64, data []byte) error
- func (this *FileWriter) WriteBodyLength(bodyLength int64) error
- func (this *FileWriter) WriteHeader(data []byte) (n int, err error)
- func (this *FileWriter) WriteHeaderLength(headerLength int) error
- type HotItem
- type Item
- type ItemType
- type ListInterface
- type Manager
- func (this *Manager) FindAllCachePaths() []string
- func (this *Manager) FindAllStorages() []StorageInterface
- func (this *Manager) FindPolicy(policyId int64) *serverconfigs.HTTPCachePolicy
- func (this *Manager) FindStorageWithPolicy(policyId int64) StorageInterface
- func (this *Manager) NewStorageWithPolicy(policy *serverconfigs.HTTPCachePolicy) StorageInterface
- func (this *Manager) TotalDiskSize() int64
- func (this *Manager) TotalMemorySize() int64
- func (this *Manager) UpdatePolicies(newPolicies []*serverconfigs.HTTPCachePolicy)
- type MemoryItem
- type MemoryList
- func (this *MemoryList) Add(hash string, item *Item) error
- func (this *MemoryList) CleanAll() error
- func (this *MemoryList) CleanMatchKey(key string) error
- func (this *MemoryList) CleanMatchPrefix(prefix string) error
- func (this *MemoryList) CleanPrefix(prefix string) error
- func (this *MemoryList) Close() error
- func (this *MemoryList) Count() (int64, error)
- func (this *MemoryList) Exist(hash string) (bool, error)
- func (this *MemoryList) IncreaseHit(hash string) error
- func (this *MemoryList) Init() error
- func (this *MemoryList) OnAdd(f func(item *Item))
- func (this *MemoryList) OnRemove(f func(item *Item))
- func (this *MemoryList) Purge(count int, callback func(hash string) error) (int, error)
- func (this *MemoryList) PurgeLFU(count int, callback func(hash string) error) error
- func (this *MemoryList) Remove(hash string) error
- func (this *MemoryList) Reset() error
- func (this *MemoryList) Stat(check func(hash string) bool) (*Stat, error)
- type MemoryReader
- func (this *MemoryReader) BodySize() int64
- func (this *MemoryReader) Close() error
- func (this *MemoryReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)
- func (this *MemoryReader) ExpiresAt() int64
- func (this *MemoryReader) HeaderSize() int64
- func (this *MemoryReader) Init() error
- func (this *MemoryReader) LastModified() int64
- func (this *MemoryReader) Read(buf []byte) (n int, err error)
- func (this *MemoryReader) ReadBody(buf []byte, callback ReaderFunc) error
- func (this *MemoryReader) ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error
- func (this *MemoryReader) ReadHeader(buf []byte, callback ReaderFunc) error
- func (this *MemoryReader) Status() int
- func (this *MemoryReader) TypeName() string
- type MemoryStorage
- func (this *MemoryStorage) AddToList(item *Item)
- func (this *MemoryStorage) CanSendfile() bool
- func (this *MemoryStorage) CanUpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy) bool
- func (this *MemoryStorage) CleanAll() error
- func (this *MemoryStorage) Delete(key string) error
- func (this *MemoryStorage) IgnoreKey(key string, maxSize int64)
- func (this *MemoryStorage) Init() error
- func (this *MemoryStorage) OpenFlushWriter(key string, expiresAt int64, status int, headerSize int, bodySize int64) (Writer, error)
- func (this *MemoryStorage) OpenReader(key string, useStale bool, isPartial bool) (Reader, error)
- func (this *MemoryStorage) OpenWriter(key string, expiredAt int64, status int, headerSize int, bodySize int64, ...) (Writer, error)
- func (this *MemoryStorage) Policy() *serverconfigs.HTTPCachePolicy
- func (this *MemoryStorage) Purge(keys []string, urlType string) error
- func (this *MemoryStorage) Stat() (*Stat, error)
- func (this *MemoryStorage) Stop()
- func (this *MemoryStorage) TotalDiskSize() int64
- func (this *MemoryStorage) TotalMemorySize() int64
- func (this *MemoryStorage) UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy)
- type MemoryWriter
- func (this *MemoryWriter) BodySize() int64
- func (this *MemoryWriter) Close() error
- func (this *MemoryWriter) Discard() error
- func (this *MemoryWriter) ExpiredAt() int64
- func (this *MemoryWriter) HeaderSize() int64
- func (this *MemoryWriter) ItemType() ItemType
- func (this *MemoryWriter) Key() string
- func (this *MemoryWriter) Write(data []byte) (n int, err error)
- func (this *MemoryWriter) WriteAt(offset int64, b []byte) error
- func (this *MemoryWriter) WriteHeader(data []byte) (n int, err error)
- type OpenFile
- type OpenFileCache
- type OpenFilePool
- type PartialFileReader
- func (this *PartialFileReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)
- func (this *PartialFileReader) Init() error
- func (this *PartialFileReader) InitAutoDiscard(autoDiscard bool) error
- func (this *PartialFileReader) MaxLength() int64
- func (this *PartialFileReader) Ranges() *PartialRanges
- type PartialFileWriter
- func (this *PartialFileWriter) AppendHeader(data []byte) error
- func (this *PartialFileWriter) BodySize() int64
- func (this *PartialFileWriter) Close() error
- func (this *PartialFileWriter) Discard() error
- func (this *PartialFileWriter) ExpiredAt() int64
- func (this *PartialFileWriter) HeaderSize() int64
- func (this *PartialFileWriter) IsNew() bool
- func (this *PartialFileWriter) ItemType() ItemType
- func (this *PartialFileWriter) Key() string
- func (this *PartialFileWriter) SetBodyLength(bodyLength int64)
- func (this *PartialFileWriter) Write(data []byte) (n int, err error)
- func (this *PartialFileWriter) WriteAt(offset int64, data []byte) error
- func (this *PartialFileWriter) WriteBodyLength(bodyLength int64) error
- func (this *PartialFileWriter) WriteHeader(data []byte) (n int, err error)
- func (this *PartialFileWriter) WriteHeaderLength(headerLength int) error
- type PartialRanges
- func (this *PartialRanges) Add(begin int64, end int64)
- func (this *PartialRanges) Bytes() []byte
- func (this *PartialRanges) Contains(begin int64, end int64) bool
- func (this *PartialRanges) Max() int64
- func (this *PartialRanges) Nearest(begin int64, end int64) (r [2]int64, ok bool)
- func (this *PartialRanges) Reset()
- func (this *PartialRanges) String() string
- func (this *PartialRanges) WriteToFile(path string) error
- type Reader
- type ReaderFunc
- type Stat
- type StorageInterface
- type Writer
Constants ¶
const ( SuffixAll = "@GOEDGE_" // 通用后缀 SuffixWebP = "@GOEDGE_WEBP" // WebP后缀 SuffixCompression = "@GOEDGE_" // 压缩后缀 SuffixCompression + Encoding SuffixMethod = "@GOEDGE_" // 请求方法后缀 SuffixMethod + RequestMethod SuffixPartial = "@GOEDGE_partial" // 分区缓存后缀 )
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 )
const ( FileStorageMaxIgnoreKeys = 32768 // 最大可忽略的键值数(尺寸过大的键值) HotItemSize = 1024 // 热点数据数量 HotItemLifeSeconds int64 = 3600 // 热点数据生命周期 FileToMemoryMaxSize = 32 * sizes.M // 可以从文件写入到内存的最大文件尺寸 FileTmpSuffix = ".tmp" DefaultMinDiskFreeSpace uint64 = 5 << 30 // 当前磁盘最小剩余空间 )
const CountFileDB = 20
Variables ¶
var ( ErrNotFound = errors.New("cache not found") ErrFileIsWriting = errors.New("the cache file is updating") ErrInvalidRange = errors.New("invalid range") ErrEntityTooLarge = errors.New("entity too large") ErrWritingQueueFull = errors.New("writing queue full") ErrServerIsBusy = errors.New("server is busy") )
常用的几个错误
Functions ¶
func NewCapacityError ¶
Types ¶
type CapacityError ¶
type CapacityError struct {
// contains filtered or unexported fields
}
CapacityError 容量错误 独立出来是为了可以在有些场合下可以忽略,防止产生没必要的错误提示数量太多
func (*CapacityError) Error ¶
func (this *CapacityError) Error() string
type FileDir ¶ added in v0.5.8
type FileDir struct { Path string Capacity *shared.SizeCapacity IsFull bool }
type FileList ¶
type FileList struct {
// contains filtered or unexported fields
}
FileList 文件缓存列表管理
func (*FileList) CleanMatchKey ¶ added in v0.5.8
CleanMatchKey 清理通配符匹配的缓存数据,类似于 https://*.example.com/hello
func (*FileList) CleanMatchPrefix ¶ added in v0.5.8
CleanMatchPrefix 清理通配符匹配的缓存数据,类似于 https://*.example.com/prefix/
func (*FileList) CleanPrefix ¶
CleanPrefix 清理某个前缀的缓存数据
func (*FileList) GetDB ¶ added in v0.5.2
func (this *FileList) GetDB(hash string) *FileListDB
func (*FileList) GetDBIndex ¶
func (*FileList) IncreaseHit ¶
IncreaseHit 增加点击量
func (*FileList) Purge ¶
Purge 清理过期的缓存 count 每次遍历的最大数量,控制此数字可以保证每次清理的时候不用花太多时间 callback 每次发现过期key的调用
type FileListDB ¶
type FileListDB struct {
// contains filtered or unexported fields
}
func NewFileListDB ¶
func NewFileListDB() *FileListDB
func (*FileListDB) AddSync ¶ added in v0.5.2
func (this *FileListDB) AddSync(hash string, item *Item) error
func (*FileListDB) CleanAll ¶
func (this *FileListDB) CleanAll() error
func (*FileListDB) CleanMatchKey ¶ added in v0.5.8
func (this *FileListDB) CleanMatchKey(key string) error
func (*FileListDB) CleanMatchPrefix ¶ added in v0.5.8
func (this *FileListDB) CleanMatchPrefix(prefix string) error
func (*FileListDB) CleanPrefix ¶
func (this *FileListDB) CleanPrefix(prefix string) error
func (*FileListDB) Close ¶
func (this *FileListDB) Close() error
func (*FileListDB) DeleteHitAsync ¶ added in v0.5.2
func (this *FileListDB) DeleteHitAsync(hash string) error
func (*FileListDB) DeleteSync ¶ added in v0.5.2
func (this *FileListDB) DeleteSync(hash string) error
func (*FileListDB) IncreaseHitAsync ¶ added in v0.5.2
func (this *FileListDB) IncreaseHitAsync(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) ListHashes ¶ added in v0.5.2
func (this *FileListDB) ListHashes(lastId int64) (hashList []string, maxId int64, 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, error)
func (*FileListDB) WrapError ¶ added in v0.5.0
func (this *FileListDB) WrapError(err error) error
type FileListHashMap ¶ added in v0.5.2
type FileListHashMap struct {
// contains filtered or unexported fields
}
FileListHashMap 文件Hash列表
func NewFileListHashMap ¶ added in v0.5.2
func NewFileListHashMap() *FileListHashMap
func (*FileListHashMap) Add ¶ added in v0.5.2
func (this *FileListHashMap) Add(hash string)
func (*FileListHashMap) AddHashes ¶ added in v0.5.2
func (this *FileListHashMap) AddHashes(hashes []string)
func (*FileListHashMap) Clean ¶ added in v0.5.2
func (this *FileListHashMap) Clean()
func (*FileListHashMap) Delete ¶ added in v0.5.2
func (this *FileListHashMap) Delete(hash string)
func (*FileListHashMap) Exist ¶ added in v0.5.2
func (this *FileListHashMap) Exist(hash string) bool
func (*FileListHashMap) IsReady ¶ added in v0.5.2
func (this *FileListHashMap) IsReady() bool
func (*FileListHashMap) Len ¶ added in v0.5.3
func (this *FileListHashMap) Len() int
func (*FileListHashMap) Load ¶ added in v0.5.2
func (this *FileListHashMap) Load(db *FileListDB) 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) 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) 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) CanSendfile ¶
func (this *FileStorage) CanSendfile() bool
CanSendfile 是否支持Sendfile
func (*FileStorage) CanUpdatePolicy ¶
func (this *FileStorage) CanUpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy) bool
CanUpdatePolicy 检查策略是否可以更新
func (*FileStorage) IgnoreKey ¶
func (this *FileStorage) IgnoreKey(key string, maxSize int64)
IgnoreKey 忽略某个Key,即不缓存某个Key
func (*FileStorage) OpenFlushWriter ¶
func (this *FileStorage) OpenFlushWriter(key string, expiresAt int64, status int, headerSize int, bodySize int64) (Writer, error)
OpenFlushWriter 打开从其他媒介直接刷入的写入器
func (*FileStorage) OpenReader ¶
func (*FileStorage) OpenWriter ¶
func (this *FileStorage) OpenWriter(key string, expiresAt int64, status int, headerSize int, bodySize 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) 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, metaHeaderSize int, metaBodySize int64, maxSize int64, endFunc func()) *FileWriter
func (*FileWriter) BodySize ¶
func (this *FileWriter) BodySize() int64
func (*FileWriter) ExpiredAt ¶
func (this *FileWriter) ExpiredAt() int64
func (*FileWriter) HeaderSize ¶
func (this *FileWriter) HeaderSize() int64
func (*FileWriter) Key ¶
func (this *FileWriter) Key() string
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 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 (*Item) RequestURI ¶ added in v0.5.8
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 // CleanMatchKey 清除通配符匹配的Key CleanMatchKey(key string) error // CleanMatchPrefix 清除通配符匹配的前缀 CleanMatchPrefix(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 MainDiskDir string SubDiskDirs []*serverconfigs.CacheDir MaxMemoryCapacity *shared.SizeCapacity // contains filtered or unexported fields }
Manager 缓存策略管理器
func (*Manager) FindAllCachePaths ¶
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) TotalMemorySize ¶
TotalMemorySize 消耗的内存尺寸
func (*Manager) UpdatePolicies ¶
func (this *Manager) UpdatePolicies(newPolicies []*serverconfigs.HTTPCachePolicy)
UpdatePolicies 重新设置策略
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) CleanAll ¶
func (this *MemoryList) CleanAll() error
func (*MemoryList) CleanMatchKey ¶ added in v0.5.8
func (this *MemoryList) CleanMatchKey(key string) error
CleanMatchKey 清理通配符匹配的缓存数据,类似于 https://*.example.com/hello
func (*MemoryList) CleanMatchPrefix ¶ added in v0.5.8
func (this *MemoryList) CleanMatchPrefix(prefix string) error
CleanMatchPrefix 清理通配符匹配的缓存数据,类似于 https://*.example.com/prefix/
func (*MemoryList) CleanPrefix ¶
func (this *MemoryList) CleanPrefix(prefix string) error
CleanPrefix 根据前缀进行清除
func (*MemoryList) Close ¶
func (this *MemoryList) Close() error
func (*MemoryList) IncreaseHit ¶
func (this *MemoryList) IncreaseHit(hash string) error
IncreaseHit 增加点击量
func (*MemoryList) Init ¶
func (this *MemoryList) Init() error
func (*MemoryList) Purge ¶
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
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) 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) Delete ¶
func (this *MemoryStorage) Delete(key string) error
Delete 删除某个键值对应的缓存
func (*MemoryStorage) IgnoreKey ¶
func (this *MemoryStorage) IgnoreKey(key string, maxSize int64)
IgnoreKey 忽略某个Key,即不缓存某个Key
func (*MemoryStorage) OpenFlushWriter ¶
func (this *MemoryStorage) OpenFlushWriter(key string, expiresAt int64, status int, headerSize int, bodySize int64) (Writer, error)
OpenFlushWriter 打开从其他媒介直接刷入的写入器
func (*MemoryStorage) OpenReader ¶
OpenReader 读取缓存
func (*MemoryStorage) OpenWriter ¶
func (this *MemoryStorage) OpenWriter(key string, expiredAt int64, status int, headerSize int, bodySize int64, maxSize int64, isPartial bool) (Writer, error)
OpenWriter 打开缓存写入器等待写入
func (*MemoryStorage) Policy ¶
func (this *MemoryStorage) Policy() *serverconfigs.HTTPCachePolicy
Policy 获取当前存储的Policy
func (*MemoryStorage) Purge ¶
func (this *MemoryStorage) Purge(keys []string, urlType string) error
Purge 批量删除缓存
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) 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 ¶
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
func (*OpenFilePool) SetClosing ¶ added in v0.6.0
func (this *OpenFilePool) SetClosing()
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 获取区间最大长度
func (*PartialFileReader) Ranges ¶ added in v0.5.8
func (this *PartialFileReader) Ranges() *PartialRanges
type PartialFileWriter ¶
type PartialFileWriter struct {
// contains filtered or unexported fields
}
func NewPartialFileWriter ¶
func (*PartialFileWriter) AppendHeader ¶
func (this *PartialFileWriter) AppendHeader(data []byte) error
func (*PartialFileWriter) BodySize ¶
func (this *PartialFileWriter) BodySize() int64
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 { Version int `json:"version"` // 版本号 Ranges [][2]int64 `json:"ranges"` // 范围 BodySize int64 `json:"bodySize"` // 总长度 }
PartialRanges 内容分区范围定义
func NewPartialRanges ¶
func NewPartialRanges(expiresAt int64) *PartialRanges
NewPartialRanges 获取新对象
func NewPartialRangesFromData ¶ added in v0.5.8
func NewPartialRangesFromData(data []byte) (*PartialRanges, error)
NewPartialRangesFromData 从数据中解析范围
func NewPartialRangesFromFile ¶
func NewPartialRangesFromFile(path string) (*PartialRanges, error)
NewPartialRangesFromFile 从文件中加载范围信息
func NewPartialRangesFromJSON ¶
func NewPartialRangesFromJSON(data []byte) (*PartialRanges, error)
NewPartialRangesFromJSON 从JSON中解析范围
func (*PartialRanges) Bytes ¶ added in v0.5.8
func (this *PartialRanges) Bytes() []byte
Bytes 将内容转换为字节
func (*PartialRanges) Contains ¶
func (this *PartialRanges) Contains(begin int64, end int64) bool
Contains 检查是否包含某个范围
func (*PartialRanges) Nearest ¶
func (this *PartialRanges) Nearest(begin int64, end int64) (r [2]int64, ok bool)
Nearest 查找最近的某个范围
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 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, headerSize int, bodySize int64, maxSize int64, isPartial bool) (Writer, error) // OpenFlushWriter 打开从其他媒介直接刷入的写入器 OpenFlushWriter(key string, expiresAt int64, status int, headerSize int, bodySize int64) (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, maxSize int64) // 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 缓存内容写入接口
Source Files
¶
- consts.go
- errors.go
- file_dir.go
- hot_item.go
- item.go
- list_file.go
- list_file_db.go
- list_file_hash_map.go
- list_interface.go
- list_memory.go
- manager.go
- open_file.go
- open_file_cache.go
- open_file_pool.go
- partial_ranges.go
- reader.go
- reader_file.go
- reader_memory.go
- reader_partial_file.go
- stat.go
- storage_file.go
- storage_interface.go
- storage_memory.go
- utils.go
- utils_partial.go
- writer.go
- writer_file.go
- writer_memory.go
- writer_partial_file.go