ramcache: stathat.com/c/ramcache Index | Files

package ramcache

import "stathat.com/c/ramcache"

Package ramcache implements an in-memory key/value cache with expirations based on access and insertion times. It is safe for concurrent use by multiple goroutines.

Index

Package Files

ramcache.go

Variables

var ErrNotFound = errors.New("ramcache: key not found in cache")

ErrNotFound is returned when a key isn't found in the cache.

func Bool Uses

func Bool(reply interface{}, err error) (bool, error)

Bool is a convenience method to type assert a Ramcache reply into a boolean value.

type Ramcache Uses

type Ramcache struct {
    TTL    time.Duration
    MaxAge time.Duration

    sync.RWMutex
    // contains filtered or unexported fields
}

Ramcache is an in-memory key/value store. It has two configuration durations: TTL (time to live) and MaxAge. Ramcache removes any objects that haven't been accessed in the TTL duration. Ramcache removes (on get) any objects that were created more than MaxAge time ago. This allows you to keep recently accessed objects cached but also delete them once they have been in the cache for MaxAge duration.

func New Uses

func New() *Ramcache

New creates a Ramcache with a TTL of 5 minutes. You can change this by setting the result's TTL to any time.Duration you want. You can also set the MaxAge on the result.

func (*Ramcache) Count Uses

func (rc *Ramcache) Count() int

Count returns the number of elements in the cache.

func (*Ramcache) CreatedAt Uses

func (rc *Ramcache) CreatedAt(key string) (t time.Time, err error)

CreatedAt returns the time the key was inserted into the cache.

func (*Ramcache) Delete Uses

func (rc *Ramcache) Delete(key string) error

Delete deletes an item from the cache.

func (*Ramcache) Each Uses

func (rc *Ramcache) Each(f func(key string, value interface{}))

Each will call f for every entry in the cache.

func (*Ramcache) Freeze Uses

func (rc *Ramcache) Freeze()

Freeze stops Ramcache from removing any expired entries.

func (*Ramcache) Get Uses

func (rc *Ramcache) Get(key string) (interface{}, error)

Get retrieves a value from the cache.

func (*Ramcache) GetNoAccess Uses

func (rc *Ramcache) GetNoAccess(key string) (interface{}, error)

GetNoAccess retrieves a value from the cache, but does not update the access time.

func (*Ramcache) Keys Uses

func (rc *Ramcache) Keys() []string

Keys returns all the keys in the cache.

func (*Ramcache) Remove Uses

func (rc *Ramcache) Remove(key string) (interface{}, error)

Remove deletes an item from the cache and returns it.

func (*Ramcache) Set Uses

func (rc *Ramcache) Set(key string, obj interface{}) error

Set inserts a value in the cache. If an object already exists, it will be replaced, but the createdAt timestamp won't change.

func (*Ramcache) Shutdown Uses

func (rc *Ramcache) Shutdown()

Shutdown cleanly stops any background work, allowing Ramcache to be garbage collected.

Package ramcache imports 5 packages (graph) and is imported by 4 packages. Updated 2018-06-23. Refresh now. Tools for package owners.