Documentation
¶
Index ¶
- Variables
- type Cache
- func (c *Cache) Adopt(key Key)
- func (c *Cache) BaseDir() string
- func (c *Cache) Delete(key Key) bool
- func (c *Cache) Get(key Key) (found bool, bytes []byte)
- func (c *Cache) Pin(key Key) (path string, release func(), ok bool)
- func (c *Cache) Set(key Key, bytes []byte) error
- func (c *Cache) SetPin(key Key, bytes []byte) (string, func(), error)
- func (c *Cache) WarmUp(walkFunc WalkFunc) error
- type CacheOption
- type Key
- type WalkFunc
- type WarmUpError
Constants ¶
This section is empty.
Variables ¶
var ( ErrEntryTooLarge = errors.New("entry size exceeds maximum allowed size") ErrCacheFull = errors.New("cache is full, unable to add new entry") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func (*Cache) Adopt ¶
Adopt attempts to take ownership of an existing file on disk and add it to the cache under the given key, without copying the file content. This is useful for cases where the file content is already on disk and can be added to the cache without needing to be written again, such as when the file is generated by an external process or when migrating existing files into the cache. note: sizing rules on entry's and cache do not apply with this function. so no eviction will occur and the entry will be added even if it exceeds maxEntrySize or maxSize.
func (*Cache) Delete ¶
Delete removes the entry associated with the given key from the cache and deletes the corresponding file from disk. Returns true if the entry was found and deleted, false if the key was not found in the cache. If the file cannot be deleted, the entry will be marked as invalid and will attempt deletion on the next access.
func (*Cache) Pin ¶
Pin marks a cache entry as in-use and returns its on-disk path. The entry cannot be evicted until Release is called. Caution: the caller must call the release function when it is done with the entry to avoid memory leaks. Caution: the file content may be modified on disk after Pin returns, so the caller should not assume the content is immutable or consistent with the content at the time of the Pin call. Use Get if you need a consistent snapshot of the content. Pin is intended for use cases where the caller needs to access the file on disk and is able to handle potential modifications to the file content, such as by re-reading the file after a modification is detected. Returns ok=false if the key is not in the cache.
func (*Cache) Set ¶
Set adds a new entry to cache or updates an existing entry size of bytes <= maxEntrySize. if current size > max size before add or updating , it will attempt to reconcile by evicting old entries until the cache is under budget if it cannot be reconciled, the new entry will not be added and an error will be returned.
func (*Cache) WarmUp ¶
WarmUp walks the cache's base directory and attempts to adopt files into the cache using the provided walkFunc to determine the corresponding cache key for each file. This is useful for pre-populating the cache with existing files on disk, such as after a restart or when initializing the cache with pre-generated content. The walkFunc should return the cache key corresponding to the given file path, and a boolean indicating whether the file should be adopted into the cache (true) or skipped (false).
type CacheOption ¶
type CacheOption func(*Cache)
func WithFileMode ¶
func WithFileMode(mode os.FileMode) CacheOption
func WithHashFunc ¶
func WithHashFunc(hashFunc func([]byte) (uint64, error)) CacheOption
func WithMaxEntrySize ¶
func WithMaxEntrySize(maxEntrySize int64) CacheOption
func WithOnEviction ¶
func WithOnEviction(callback func(key Key)) CacheOption
type WarmUpError ¶
type WarmUpError struct {
Errs []error
}
func (*WarmUpError) Error ¶
func (e *WarmUpError) Error() string