blobcache

package
v0.0.0-...-bd9ca74 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

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.

func (EmptyCache) Size

func (EmptyCache) Size() int64

Size returns 0.

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

func NewLRU(s store.Store, maxSize int64) *StoreLRU

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

func (t *StoreLRU) Contains(key string) bool

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

func (t *StoreLRU) Delete(key string) error

Delete removed an item from the cache. It is not an error to remove a key which is not present.

func (*StoreLRU) Get

func (t *StoreLRU) Get(key string) (store.ReadAtCloser, int64, error)

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

func (t *StoreLRU) MaxSize() int64

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.)

func (*StoreLRU) Scan

func (t *StoreLRU) Scan()

Scan enumerates the items in the given store and enters them into the LRU cache (if they aren't in it already).

func (*StoreLRU) Size

func (t *StoreLRU) Size() int64

Size returns the amount currently used by the cache in bytes.

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

func NewTime(s store.Store, d time.Duration) *TimeBased

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

func (te *TimeBased) Contains(key string) bool

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) Delete

func (te *TimeBased) Delete(key string) error

Delete removes the given item from the cache.

func (*TimeBased) Get

func (te *TimeBased) Get(key string) (store.ReadAtCloser, int64, error)

Get returns a reader for reading the content stored at the given key.

func (*TimeBased) MaxSize

func (te *TimeBased) MaxSize() int64

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.

func (*TimeBased) Size

func (te *TimeBased) Size() int64

Size returns the amount of storage currently used by the cache in bytes.

func (*TimeBased) Stop

func (te *TimeBased) Stop()

Stop will stop the background goroutine that was spawned in NewTime().

(Is there a better name than `Stop`?)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL