rcache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2014 License: MIT Imports: 2 Imported by: 21

README

rcache

rcache is a simple cache that periodically erases stale items and which can help mitigate the thundering herd problem. rcache's growth is unbound. It is meant to cache a small number of items.

Usage:

// thread-safe
apps := rcache.New(fetcher, time.Minute * 2)

func fetcher(key string) interface{} {
  // HIT YOUR DB, GET THE APP
  // ...
  return theApp
}

// in code:
app := apps.Get("spice").(*Application)

There's a short 20 second grace window in which an expired item will be returned. In other words, the real TTL of items placed in the above cache is 120-140seconds. Even if multiple goroutines concurrently GET an item which is in this grace window, only 1 call to fetcher will be executed.

Methods

  • Get(key string) - Returns the value or nil. Either gets it from the cache or fetches it via the fetcher
  • Set(key string, value interface{}) - Sets the value
  • Replace(key string, value interface{}) - Replaces an existing value, extending it's TTL. Noop if key isn't already in the cache
  • Delete(key string) - Deletes an item
  • Clear() - removes all items from the cache

Integer Keys

By default, the cache key is a string. You can create a cache that uses an integer key by using NewInt (instead of New). Everything works the same, except keys are int.

LRU Cache Alternative

For a more powerful LRU cache, checkout ccache

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GRACE_LIMIT      = time.Second * 20
	FETCH_TIME_LIMIT = time.Second * 5
	REAPER_FREQUENCY = time.Minute * 5
	REAPER_LIMIT     = 1000
)

Functions

This section is empty.

Types

type Cache

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

func New

func New(fetcher Fetcher, ttl time.Duration) *Cache

func (*Cache) Clear

func (c *Cache) Clear()

func (*Cache) Delete

func (c *Cache) Delete(key string)

func (*Cache) Get

func (c *Cache) Get(key string) interface{}

func (*Cache) Replace

func (c *Cache) Replace(key string, value interface{})

func (*Cache) Set

func (c *Cache) Set(key string, value interface{})

type Fetcher

type Fetcher func(key string) interface{}

type IntCache

type IntCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewInt

func NewInt(fetcher IntFetcher, ttl time.Duration) *IntCache

func (*IntCache) Clear

func (c *IntCache) Clear()

func (*IntCache) Delete

func (c *IntCache) Delete(key int)

func (*IntCache) Get

func (c *IntCache) Get(key int) interface{}

func (*IntCache) Replace

func (c *IntCache) Replace(key int, value interface{})

func (*IntCache) Set

func (c *IntCache) Set(key int, value interface{})

type IntFetcher

type IntFetcher func(key int) interface{}

type Item

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

func (*Item) State

func (i *Item) State() state

Jump to

Keyboard shortcuts

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