Documentation ¶
Index ¶
- Variables
- type Cache
- func (ca *Cache) Capacity() uint64
- func (ca *Cache) Close()
- func (ca *Cache) Get(key string) (*CacheObj, bool)
- func (ca *Cache) Keys() []string
- func (ca *Cache) LoadOrStore(key string) (*CacheObj, bool)
- func (ca *Cache) NewObj(key string) *CacheObj
- func (ca *Cache) Peek(key string) (*CacheObj, bool)
- func (ca *Cache) Size() uint64
- type CacheObj
Constants ¶
This section is empty.
Variables ¶
var BufferBlockSleepInterval = time.Millisecond * 10
var ErrNoUnreadBuffers = errors.New("could not get an unread buffer")
var ErrNothingToRemove = errors.New("no buffers")
var MaxBufferBlockTries = 100 * 10
var MaxBufferReturnTries = 1000
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func (*Cache) Get ¶
Get returns the object and whether it exists. The object is bumped to the top of the LRU.
func (*Cache) LoadOrStore ¶
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 ¶
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.
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.