Documentation ¶
Overview ¶
Package diskcache is a simple local-disk cache implements.
The diskcache package is a local-disk cache, it implements following functions:
- Concurrent Put()/Get().
- Recoverable last-read-position on restart.
- Exclusive Open() on same path.
- Errors during Get() are retriable.
- Auto-rotate on batch size.
- Drop in FIFO policy when max capacity reached.
- We can configure various specifics in environments without to modify options source code.
Index ¶
- Constants
- Variables
- func Metrics() []prometheus.Collector
- func ResetMetrics()
- type CacheOption
- func WithBatchSize(size int64) CacheOption
- func WithCapacity(size int64) CacheOption
- func WithDirPermission(perms os.FileMode) CacheOption
- func WithExtraCapacity(size int64) CacheOption
- func WithFilePermission(perms os.FileMode) CacheOption
- func WithMaxDataSize(size int32) CacheOption
- func WithNoFallbackOnError(on bool) CacheOption
- func WithNoLock(on bool) CacheOption
- func WithNoPos(on bool) CacheOption
- func WithNoSync(on bool) CacheOption
- func WithPath(x string) CacheOption
- func WithWakeup(wakeup time.Duration) CacheOption
- type DiskCache
- type Fn
Constants ¶
const ( // EOFHint labels a file's end. EOFHint = uint32(0xdeadbeef) )
Variables ¶
var ( // Invalid read size. ErrUnexpectedReadSize = errors.New("unexpected read size") // Data send to Put() exceed the maxDataSize. ErrTooLargeData = errors.New("too large data") // Get on no data cache. ErrEOF = errors.New("EOF") // Invalid cache filename. ErrInvalidDataFileName = errors.New("invalid datafile name") ErrInvalidDataFileNameSuffix = errors.New("invalid datafile name suffix") // Invalid file header. ErrBadHeader = errors.New("bad header") )
Generic diskcache errors.
Functions ¶
func Metrics ¶
func Metrics() []prometheus.Collector
Types ¶
type CacheOption ¶
type CacheOption func(c *DiskCache)
A CacheOption used to set various options on DiskCache.
func WithBatchSize ¶
func WithBatchSize(size int64) CacheOption
WithBatchSize set file size, default 64MB.
func WithCapacity ¶
func WithCapacity(size int64) CacheOption
WithCapacity set cache capacity, default unlimited.
func WithDirPermission ¶
func WithDirPermission(perms os.FileMode) CacheOption
WithDirPermission set disk dir permission mode.
func WithExtraCapacity ¶
func WithExtraCapacity(size int64) CacheOption
WithExtraCapacity add capacity to existing cache.
func WithFilePermission ¶
func WithFilePermission(perms os.FileMode) CacheOption
WithFilePermission set cache file permission mode.
func WithMaxDataSize ¶
func WithMaxDataSize(size int32) CacheOption
WithMaxDataSize set max single data size, default 32MB.
func WithNoFallbackOnError ¶
func WithNoFallbackOnError(on bool) CacheOption
WithNoFallbackOnError disable fallback on fn() error.
During Get(fn(data []btye)error{...}), if fn() failed with error, the next Get still get the same data from cache. If fallback disabled, the next read will read new data from cache, and the previous failed data skipped(and eventually dropped).
func WithNoLock ¶
func WithNoLock(on bool) CacheOption
WithNoLock set .lock on or off.
File '.lock' used to exclude Open() on same path.
func WithNoPos ¶
func WithNoPos(on bool) CacheOption
WithNoPos set .pos on or off.
The file '.pos' used to remember last Get() position, without '.pos', on process restart, some already-Get() data will Get() again in the new process, this maybe not the right action we expect.
func WithNoSync ¶
func WithNoSync(on bool) CacheOption
WithNoSync enable/disable sync on cache write.
NOTE: Without sync, the write performance 60~80 times faster for 512KB/1MB put, for smaller put will get more faster(1kb for 4000+ times).
func WithWakeup ¶
func WithWakeup(wakeup time.Duration) CacheOption
WithWakeup set duration on wakeup(default 3s), this wakeup time used to shift current-writing-file to ready-to-reading-file.
NOTE: without wakeup, current-writing-file maybe not read-available for a long time.
type DiskCache ¶
type DiskCache struct {
// contains filtered or unexported fields
}
DiskCache is the representation of a disk cache. A DiskCache is safe for concurrent use by multiple goroutines. Do not Open the same-path diskcache among goroutines.
func Open ¶
func Open(opts ...CacheOption) (*DiskCache, error)
Open init and create a new disk cache. We can set other options with various options.
func (*DiskCache) Close ¶
Close reclame fd resources. Close is safe to call concurrently with other operations and will block until all other operations finish.
func (*DiskCache) Get ¶
Get fetch new data from disk cache, then passing to fn if any error occurred during call fn, the reading data is dropped, and will not read again.
Get is safe to call concurrently with other operations and will block until all other operations finish.
func (*DiskCache) Put ¶
Put write @data to disk cache, if reached batch size, a new batch is rotated. Put is safe to call concurrently with other operations and will block until all other operations finish.