cache

package
v0.0.0-...-8107f4d Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BufferBlockSleepInterval = time.Millisecond * 10
View Source
var ErrNoUnreadBuffers = errors.New("could not get an unread buffer")
View Source
var ErrNothingToRemove = errors.New("no buffers")
View Source
var MaxBufferBlockTries = 100 * 10
View Source
var MaxBufferReturnTries = 1000

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

func New

func New(sizeBytes uint64, blockSizeBytes uint64) *Cache

func (*Cache) Capacity

func (ca *Cache) Capacity() uint64

Capacity returns the capacity of the cache in bytes.

func (*Cache) Close

func (ca *Cache) Close()

Close implements io.Closer and other interfaces.

func (*Cache) Get

func (ca *Cache) Get(key string) (*CacheObj, bool)

Get returns the object and whether it exists. The object is bumped to the top of the LRU.

func (*Cache) Keys

func (ca *Cache) Keys() []string

Keys returns the keys in the cache.

func (*Cache) LoadOrStore

func (ca *Cache) LoadOrStore(key string) (*CacheObj, bool)

LoadOrStore gets the CacheObj for key, or creates a new one if it doesn't exist. Returns the object, and whether it was loaded. The fetching and creation is atomic for the given key.

func (*Cache) NewObj

func (ca *Cache) NewObj(key string) *CacheObj

NewObject returns a new object for reading and writing. The object will be immediately stored in the Cache, and is immediately available to clients calling Get.

Note this is synchronized with Get. That is, if two goroutines simultaneously call NewObject and Get

However, be aware there is a race condition, if a reader comes in at the exact same time NewObject is called, which will result in multiple clients thinking there is no object in the cache and fetching from the origin.

Therefore, callers should synchronize NewObject and Get calls. They probably also need to synchronize the full "Get, then check if the object can be reused, and if not create a new object".

There is also no synchronization around whatever is put in MetaData. Therefore, callers must do their own synchronization if whatever is put in CacheObj.MetaData is read or written by multiple goroutines.

func (*Cache) Peek

func (ca *Cache) Peek(key string) (*CacheObj, bool)

Peek returns the object and whether it exists, without bumping the object to the top of the LRU.

func (*Cache) Size

func (ca *Cache) Size() uint64

Size always returns the capacity. The FreeList pre-allocates, and determining the used block bytes can't be done quickly.

type CacheObj

type CacheObj struct {
	// Buf is the bytes storage of the cache, which uses the FreeList to reduce GC load.
	Buf freelist.Buffer
	// MetaData may be any metadata necessary. This will be stored, but does not use freelist.FreeList.
	// Therefore, all data possible should be stored in Buf.
	// Only data that absolutely requires nonlinear access by the application itself should be stored in MetaData.
	//
	// TODO verify this doesn't need to be a pointer
	MetaData interface{}
	HitCount uint64 // the number of times this object was requested from the cache
}

CacheObj is an object stored in the Cache. It contains a Buf for bytes reading and writing, And a MetaData for structured non-freelist metadata.

TODO change to only allow access to buffer.NewReader, so only the NewObj caller can Write.

Jump to

Keyboard shortcuts

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