package ramcache

import "stathat.com/c/ramcache"

Install: go get 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.

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

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

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

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

func (rc *Ramcache) Count() int

Count returns the number of elements in the cache.

func (*Ramcache) CreatedAt

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

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

Delete deletes an item from the cache.

func (*Ramcache) Each

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

Each will call f for every entry in the cache.

func (*Ramcache) Freeze

func (rc *Ramcache) Freeze()

Freeze stops Ramcache from removing any expired entries.

func (*Ramcache) Get

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

Get retrieves a value from the cache.

func (*Ramcache) GetNoAccess

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

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

Keys returns all the keys in the cache.

func (*Ramcache) Remove

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

Remove deletes an item from the cache and returns it.

func (*Ramcache) Set

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.