Documentation
¶
Overview ¶
Package filecache provides a LRU file caching mechanism to cache resources to the local disk that take a long time to generate or download from the network.
Index ¶
- Constants
- Variables
- type ByteSliceKey
- type Cache
- type Config
- type CreateFunc
- type File
- func (f *File[_]) Close() error
- func (f *File[_]) Name() string
- func (f *File[_]) OSFile() *os.File
- func (f *File[_]) Read(b []byte) (int, error)
- func (f *File[_]) ReadAt(b []byte, off int64) (int, error)
- func (f *File[_]) Seek(offset int64, whence int) (int64, error)
- func (f *File[K]) Stat() (os.FileInfo, error)
- type FileInfo
- type Hash
- type Key
- type Logger
- type Status
- type StringKey
- type Uint32Key
- type Uint64Key
Constants ¶
const HashSize = 32
HashSize is the size, in bytes, of the key hash.
Variables ¶
var ErrInternal = errors.New("internal error")
ErrInternal is the error thrown when an internal error occurred.
var ErrInvalidConfig = errors.New("invalid config")
ErrInvalidConfig is the error thrown when the passed configuration parameter is not valid.
Functions ¶
This section is empty.
Types ¶
type ByteSliceKey ¶
type ByteSliceKey []byte
ByteSliceKey is a wrapper type to attach the Hash method to []byte.
func (ByteSliceKey) Hash ¶
func (k ByteSliceKey) Hash() Hash
Hash calculates and returns a hash value of the []byte using SHA-512/256.
func (ByteSliceKey) String ¶
func (k ByteSliceKey) String() string
String returns the string representation of the []byte key.
type Cache ¶
type Cache[K Key] struct { // contains filtered or unexported fields }
Cache represents the file cache directory.
func New ¶
func New[K Key](dir string, create CreateFunc[K]) (*Cache[K], error)
New create a cache with the default configuration.
func NewWithConfig ¶
NewWithConfig create a cache using the given configuration parameters.
func (*Cache[K]) Get ¶
Get gets the file for the key from the cache. If the file for the specified key does not exist in the cache, it will call the CreateFunc to create the new file. It returns the file opened for read, cached or not.
The returned file is guaranteed to remain referenced until it is closed and not removed from the cache during that time. After using the file, it is the caller's responsibility to call the File.Close() method of the returned file. Otherwise the file will remain in the cache, and the reference will remain in the memory.
type Config ¶
type Config[K Key] struct { // The path to the directory for cache files. It should be a dedicated // directory used exclusively for this cache. The directory will be // automatically created if it does not exist. Both absolute and // relative paths are allowed. A relative path is treated as relative // from the user-specific cache directory returned by os.UserCacheDir(). // If it is empty, use the program name directory. Dir string // The callback function that is called when a not-cached resource is // requested. Create CreateFunc[K] // The upper limit on the number of files that can be cached. Zero // value means unlimited. When more than this number of files are // cached, the oldest files will be removed. Note that more than this // number of files may be cached temporarily. MaxFiles uint64 // The limit on the total size of files that can be cached. Zero // value means unlimited. When more than this total size of files are // cached, the oldest files will be removed. Note that more than this // size of files may be cached temporarily. There is no guarantee that // more disk space than this will not be used. MaxSize infounit.ByteCount // The maximum age of cache files. Note that it is the time since // last access, not the time since creation. Also the cache is not // removed immediately after this age. It is still possible that an // aged cache file will continue to be hit and reused. Zero value // means unlimited. MaxAge time.Duration // The interval between GC processing to find and remove old cache // files that exceed the configured limits. GCInterval time.Duration // If not nil, Cache outputs log messages to this Logger object. Logger Logger // If true, Cache outputs debug log messages. Only effective if // Logger is not nil. DebugLog bool }
Config represents the parameters to configure Cache creation.
type CreateFunc ¶
CreateFunc represents a function for file creation. It will be called when Get is called for a new key that does not exist in the cache. It should create the file using the pre-opened file argument passed. It does not have to close the file, while it will be closed automatically after return.
type File ¶
type File[K Key] struct { // contains filtered or unexported fields }
File represents the cached file returned by Get() method.
func (*File[_]) OSFile ¶
OSFile returns the underlying os.File object. Use with caution, as all writes will fail and operations such as deleting, renaming or closing the file will cause unexpected results. Only use if it is required to pass the file to a package that accepts only os.File or the file descriptor. Also use File.Close instead of os.File.Close even if this method is called.
type FileInfo ¶
type FileInfo[K Key] struct { // contains filtered or unexported fields }
FileInfo implements fs.FileInfo interface.
func (*FileInfo[_]) ModTime ¶
ModTime returns the last access time or created time of the cache entry.
type Logger ¶
type Logger interface {
FileCacheLog(string)
}
Logger is the interface implemented to receive log messages from the running Cache instance.
type Status ¶
type Status struct { NumFiles uint64 // number of files currently in cache. TotalSize infounit.ByteCount // total size of files currently in cache. NumRequested uint64 // total number of files requested. NumHit uint64 // total number of cache hits. NumCreated uint64 // total number of newly created cache files. NumFailed uint64 // total number of operation failures. NumRemoved uint64 // total number of removed cache files. NumOps int // number of operations currently being processed. NumRefs int // number of currently referenced cache files. }
Status represents the cache status and statistics.
type StringKey ¶
type StringKey string
StringKey is a wrapper type to attach the Hash method to string.
type Uint32Key ¶
type Uint32Key uint32
Uint32Key is a wrapper type to attach the Hash method to uint32.