cache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2021 License: MIT Imports: 8 Imported by: 3

README

cache package

Cache is a simple in-memory k/v store that allows for custom OnExpirefunctions and a Refresh option to allow for lru-like behavior.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCollision is a hash collision error
	ErrCollision = errors.New("hash collision")
	// ErrDNE is a "does not exist" error
	ErrDNE = errors.New("does not exist")
)

Functions

This section is empty.

Types

type Bucket added in v0.1.0

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

Bucket indexes a group of keys in cache and should be used to manage them

func (*Bucket) Add added in v0.1.0

func (b *Bucket) Add(key string, item interface{}, expiresIn time.Duration) error

Add will add an item to the bucket.

func (*Bucket) Delete added in v0.1.0

func (b *Bucket) Delete(key string) error

Delete will remove an item from the bucket

func (*Bucket) Extend added in v0.1.0

func (b *Bucket) Extend(key string, extend time.Duration) error

Extend will extend an item from the bucket.

func (*Bucket) Get added in v0.1.0

func (b *Bucket) Get(key string) (interface{}, error)

Get will get an item from the bucket.

func (*Bucket) Iterator added in v0.1.0

func (b *Bucket) Iterator() *bucketIterator

Iterator will return an iterator to iterate over the items in the bucket.

func (*Bucket) Len added in v0.1.0

func (b *Bucket) Len() int

Len returns the number of items in the bucket

func (*Bucket) Update added in v0.1.0

func (b *Bucket) Update(key string, item interface{}) error

Update will update the item in the bucket

type Cache

type Cache struct {
	*sync.Mutex
	// contains filtered or unexported fields
}

Cache is a generic in-memory cache

func NewCache

func NewCache(config *CacheConfig) *Cache

NewCache will create and return a pointer to a new Cache object Renewable sets whether

func (*Cache) Add

func (t *Cache) Add(key string, item interface{}, expiresIn time.Duration) error

Add will add a key, value, and expiration duration to the cache. If the key already exists in the collision (i.e. if a collision occurs) then an ErrCollision value will be returned. If you use an expiresIn time of `0` then the item will never be expired from the cache.

func (*Cache) Bucket added in v0.1.0

func (c *Cache) Bucket(name string) *Bucket

Bucket will return the bucket if it exists. It will create and return a new bucket by the name if the bucket does not already exist.

func (*Cache) Delete

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

Delete will delete a key from the cache. It will return ErrDNE if the key does not exist.

func (*Cache) Extend

func (t *Cache) Extend(key string, extend time.Duration) error

Extend will extend the time until expiration for the specified key by the specified duration.

func (*Cache) Get

func (t *Cache) Get(key string) (interface{}, error)

Get will return the value stored at the key. It will return an ErrDNE value if key is not in cache.

func (*Cache) Load

func (c *Cache) Load(filename string) error

Load will load an empty cache with the data from the given file. File should contain a gob encoded cached object created via the `Save()` method.

func (*Cache) Save

func (c *Cache) Save(filename string) error

Save will gob-encode and persist the cache in its current state to a file of the given name.

func (*Cache) Update

func (t *Cache) Update(key string, item interface{}) error

Update updates the value at the key to the new supplied value

type CacheConfig

type CacheConfig struct {
	OnExpires       OnExpires
	Refresh         bool // extends key's expiration time on usage (for lru-like behavior)
	RefreshDuration time.Duration
	CleanDuration   time.Duration
}

CacheConfig is used to configure a cache

type OnExpires

type OnExpires func(item interface{})

OnExpires is a function that will act on the item object of an expired Slot.

type Slot

type Slot struct {
	Item      interface{}
	ExpiresAt time.Time
	// contains filtered or unexported fields
}

Slot is a slot in a cache

Jump to

Keyboard shortcuts

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