Documentation ¶
Overview ¶
Package blobcache implements a simple cache. It is backed by a store, so it can be entirely in memory or disk-backed.
While the cached contents are kept in the store, the list recording usage information is kept only in memory. On startup the items in the store are enumerated and taken to populate the cache list in an undetermined order.
The only cache implemented in here uses an LRU item replacement policy. It would be nice for it to use an ARC or 2Q policy instead.
Index ¶
- Variables
- type EmptyCache
- type StoreLRU
- func (t *StoreLRU) Contains(key string) bool
- func (t *StoreLRU) Delete(key string) error
- func (t *StoreLRU) Get(key string) (store.ReadAtCloser, int64, error)
- func (t *StoreLRU) MaxSize() int64
- func (t *StoreLRU) Put(key string) (io.WriteCloser, error)
- func (t *StoreLRU) Scan()
- func (t *StoreLRU) Size() int64
- type T
- type TimeBased
- func (te *TimeBased) Contains(key string) bool
- func (te *TimeBased) Delete(key string) error
- func (te *TimeBased) Get(key string) (store.ReadAtCloser, int64, error)
- func (te *TimeBased) MaxSize() int64
- func (te *TimeBased) Put(key string) (io.WriteCloser, error)
- func (te *TimeBased) Scan()
- func (te *TimeBased) Size() int64
- func (te *TimeBased) Stop()
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCacheFull means the item being added to the cache is too big // for the cache. ErrCacheFull = errors.New("Cache is full and no more items can be removed") ErrPutPending = errors.New("Key is already being added to cache") )
Functions ¶
This section is empty.
Types ¶
type EmptyCache ¶
type EmptyCache struct{}
An EmptyCache always misses. It contains nothing and saves nothing.
func (EmptyCache) Contains ¶
func (EmptyCache) Contains(key string) bool
Contains always returns false.
func (EmptyCache) Delete ¶
func (EmptyCache) Delete(key string) error
Delete removes an item from the cache. (In this case it's always a nop).
func (EmptyCache) Get ¶
func (EmptyCache) Get(key string) (store.ReadAtCloser, int64, error)
Get always returns a cache miss.
func (EmptyCache) MaxSize ¶
func (EmptyCache) MaxSize() int64
MaxSize will return 0 (which means the cache size is infinite).
func (EmptyCache) Put ¶
func (EmptyCache) Put(key string) (io.WriteCloser, error)
Put returns a valid WriteCloser which discards its input. The item being added will ultimately not be added to the cache.
type StoreLRU ¶
type StoreLRU struct {
// contains filtered or unexported fields
}
A StoreLRU implements a cache using the least recently used (LRU) eviction policy and using a store as the storage backend.
func NewLRU ¶
NewLRU creates and initializes a new cache structure using the least recently used eviction policy. The given store may already have items in it. You must call Scan() (either inline or in another goroutine) to add the preexisting items in the store to the LRU list.
func (*StoreLRU) Contains ¶
Contains returns true if the given item is in the cache. It does not update the LRU status, and does not guarantee the item will be in the cache when Get() is called.
func (*StoreLRU) Delete ¶
Delete removed an item from the cache. It is not an error to remove a key which is not present.
func (*StoreLRU) Get ¶
Get returns a reader for the given item. It will block until the data is ready to be read. The LRU list is updated. If the item is not in the cache nil is returned for the ReadAtCloser. (NOTE: it is not an error for an item to not be in the cache. Check the ReadAtCloser to see.)
func (*StoreLRU) MaxSize ¶
MaxSize returns the maximum size of this cache in bytes. (Not the current size of the cache.)
func (*StoreLRU) Put ¶
func (t *StoreLRU) Put(key string) (io.WriteCloser, error)
Put returns a WriteCloser which saves writes to it in the cache under the provided key. Items are evicted from the cache as content is written to the Writer. The item is not formally added to the cache until the Writer is closed.
Only one writer to a given key can be active at a time. Subsequent Puts will return an error. Also, once an item is in the cache, Puts for it will return an error (until the item is evicted.)
type T ¶
type T interface { // Is the given key in the cache? Contains(key string) bool // Get content associated with a key from the cache. Get(key string) (store.ReadAtCloser, int64, error) // Add content into the cache Put(key string) (io.WriteCloser, error) // Remove an entry from the cache Delete(key string) error // The amount currently stored in the cache Size() int64 // The maximum size of the cache in bytes. (0 if there is no max size) MaxSize() int64 }
T is the basic blob cache interface.
type TimeBased ¶
type TimeBased struct {
// contains filtered or unexported fields
}
A TimeBased cache will keep items for a fixed length of time after their last access. If they are accessed again in that period of time, their expiration clock is reset. Items whose expiration clock expires will be removed from the cache.
The total amount of storage used by this cache will vary over time, and may grow without bound.
func NewTime ¶
NewTime returns a new time-based cache using s as the backing store and with items having a time-to-live of duration d.
func (*TimeBased) Contains ¶
Contains returns true if the given key is in the cache when the function is called. The key may be removed in between calling Contains() and Get().
func (*TimeBased) MaxSize ¶
MaxSize always returns 0 since there is no size limit for a TimeBased cache.
func (*TimeBased) Put ¶
func (te *TimeBased) Put(key string) (io.WriteCloser, error)
Put returns a writer for saving the content of the given key. The item is added to the cache when the writer is closed. The error `ErrPutPending` is returned if someone else is currently saving content to the key. If the item is already in the cache, it is deleted first.
func (*TimeBased) Scan ¶
func (te *TimeBased) Scan()
Scan will scan the backing store for items and also try to load previous expire times from a cache file. An updated index file is saved.