Categorygithub.com/koding/cache
modulepackage
0.0.0-20161222233015-e8a81b0b3f20
Repository: https://github.com/koding/cache.git
Documentation: pkg.go.dev

# README

Cache GoDoc Build Status

Cache is a backend provider for common use cases

Install and Usage

Install the package with:

go get github.com/koding/cache

Import it with:

import "github.com/koding/cache"

Example


// create a cache with 2 second TTL
cache := NewMemoryWithTTL(2 * time.Second)
// start garbage collection for expired keys
cache.StartGC(time.Millisecond * 10)
// set item
err := cache.Set("test_key", "test_data")
// get item
data, err := cache.Get("test_key")

Supported caching algorithms:

  • MemoryNoTS : provides a non-thread safe in-memory caching system
  • Memory : provides a thread safe in-memory caching system, built on top of MemoryNoTS cache
  • LRUNoTS : provides a non-thread safe, fixed size in-memory caching system, built on top of MemoryNoTS cache
  • LRU : provides a thread safe, fixed size in-memory caching system, built on top of LRUNoTS cache
  • MemoryTTL : provides a thread safe, expiring in-memory caching system, built on top of MemoryNoTS cache
  • ShardedNoTS : provides a non-thread safe sharded cache system, built on top of a cache interface
  • ShardedTTL : provides a thread safe, expiring in-memory sharded cache system, built on top of ShardedNoTS over MemoryNoTS
  • LFUNoTS : provides a non-thread safe, fixed size in-memory caching system, built on top of MemoryNoTS cache
  • LFU : provides a thread safe, fixed size in-memory caching system, built on top of LFUNoTS cache

# Functions

MustEnsureIndexExpireAt ensures the expireAt index usage: NewMongoCacheWithTTL(mongoSession, MustEnsureIndexExpireAt()).
NewLFU creates a thread-safe LFU cache.
NewLFUNoTS creates a new LFU cache struct for further cache operations.
NewLRU creates a thread-safe LRU cache.
NewLRUNoTS creates a new LRU cache struct for further cache operations.
NewMemNoTSCache is a helper method to return a Cache interface, so callers don't have to typecast.
NewMemory creates an inmemory cache system Which everytime will return the true value about a cache hit.
NewMemoryNoTS creates MemoryNoTS struct.
NewMemoryWithTTL creates an inmemory cache system Which everytime will return the true values about a cache hit and never will leak memory ttl is used for expiration of a key from cache.
NewMongoCacheWithTTL creates a caching layer backed by mongo.
NewShardedCacheWithTTL creates a sharded cache system with TTL based on specified Cache constructor Which everytime will return the true values about a cache hit and never will leak memory ttl is used for expiration of a key from cache.
NewShardedNoTS inits ShardedNoTS struct.
NewShardedWithTTL creates an in-memory sharded cache system ttl is used for expiration of a key from cache.
SetCollectionName sets the collection name for mongoDB in MongoCache struct as option usage: NewMongoCacheWithTTL(mongoSession, SetCollectionName("mongoCollName")).
SetGCInterval sets the garbage collector interval in MongoCache struct as option usage: NewMongoCacheWithTTL(mongoSession, SetGCInterval(time*Minute)).
SetTTL sets the ttl duration in MongoCache as option usage: NewMongoCacheWithTTL(mongoSession, SetTTL(time*Minute)).
StartGC enables the garbage collector in MongoCache struct usage: NewMongoCacheWithTTL(mongoSession, StartGC()).

# Variables

ErrNotFound holds exported `not found error` for not found items.

# Structs

Document holds the key-value pair for mongo cache.
LFU holds the Least frequently used cache values.
LFUNoTS holds the cache struct.
LRU Discards the least recently used items first.
LRUNoTS Discards the least recently used items first.
Memory provides an inmemory caching mechanism.
MemoryNoTS provides a non-thread safe caching mechanism.
MemoryTTL holds the required variables to compose an in memory cache system which also provides expiring key mechanism.
MongoCache holds the cache values that will be stored in mongoDB.
ShardedNoTS ; the concept behind this storage is that each cache entry is associated with a tenantID and this enables fast purging for just that tenantID.
ShardedTTL holds the required variables to compose an in memory sharded cache system which also provides expiring key mechanism.

# Interfaces

Cache is the contract for all of the cache backends that are supported by this package.
ShardedCache is the contract for all of the sharded cache backends that are supported by this package.

# Type aliases

Option sets the options specified.