{"modulePath":"github.com/koding/cache","version":"v0.0.0-20161222233015-e8a81b0b3f20","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cache\n\nPackage cache provides basic caching mechanisms for Go(lang) projects.\n\nCurrently supported caching algorithms:\n\n\tMemoryNoTS  : provides a non-thread safe in-memory caching system\n\tMemory      : provides a thread safe in-memory caching system, built on top of MemoryNoTS cache\n\tLRUNoTS     : provides a non-thread safe, fixed size in-memory caching system, built on top of MemoryNoTS cache\n\tLRU         : provides a thread safe, fixed size in-memory caching system, built on top of LRUNoTS cache\n\tMemoryTTL   : provides a thread safe, expiring in-memory caching system,  built on top of MemoryNoTS cache\n\tShardedNoTS : provides a non-thread safe sharded cache system, built on top of a cache interface\n\tShardedTTL  : provides a thread safe, expiring in-memory sharded cache system, built on top of ShardedNoTS over MemoryNoTS\n\tLFUNoTS     : provides a non-thread safe, fixed size in-memory caching system, built on top of MemoryNoTS cache\n\tLFU         : provides a thread safe, fixed size in-memory caching system, built on top of LFUNoTS cache\n\n## Variables\n\n```go\nvar (\n\t// ErrNotFound holds exported `not found error` for not found items\n\tErrNotFound = errors.New(\"not found\")\n)\n```\n\n## Types\n\n```go\ntype Cache interface {\n\t// Get returns single item from the backend if the requested item is not\n\t// found, returns NotFound err\n\tGet(key string) (interface{}, error)\n\n\t// Set sets a single item to the backend\n\tSet(key string, value interface{}) error\n\n\t// Delete deletes single item from backend\n\tDelete(key string) error\n}\n```\nCache is the contract for all of the cache backends that are supported by this package\n\n```go\nfunc NewLFU(size int) Cache\n```\nNewLFU creates a thread-safe LFU cache\n\n```go\nfunc NewLFUNoTS(size int) Cache\n```\nNewLFUNoTS creates a new LFU cache struct for further cache operations. Size is used for limiting the upper bound of the cache\n\n```go\nfunc NewLRU(size int) Cache\n```\nNewLRU creates a thread-safe LRU cache\n\n```go\nfunc NewLRUNoTS(size int) Cache\n```\nNewLRUNoTS creates a new LRU cache struct for further cache operations. Size is used for limiting the upper bound of the cache\n\n```go\nfunc NewMemNoTSCache() Cache\n```\nNewMemNoTSCache is a helper method to return a Cache interface, so callers don't have to typecast\n\n```go\nfunc NewMemory() Cache\n```\nNewMemory creates an inmemory cache system Which everytime will return the true value about a cache hit\n\n```go\ntype Document struct {\n\tKey      string      `bson:\"_id\" json:\"_id\"`\n\tValue    interface{} `bson:\"value\" json:\"value\"`\n\tExpireAt time.Time   `bson:\"expireAt\" json:\"expireAt\"`\n}\n```\nDocument holds the key-value pair for mongo cache\n\n```go\ntype LFU struct {\n\t// Mutex is used for handling the concurrent\n\t// read/write requests for cache\n\tsync.Mutex\n\t// contains filtered or unexported fields\n}\n```\nLFU holds the Least frequently used cache values\n\n```go\nfunc (l *LFU) Delete(key string) error\n```\nDelete deletes the given key-value pair from cache, this function doesnt return an error if item is not in the cache\n\n```go\nfunc (l *LFU) Get(key string) (interface{}, error)\n```\nGet returns the value of a given key if it exists, every get item will be increased for every usage\n\n```go\nfunc (l *LFU) Set(key string, val interface{}) error\n```\nSet sets or overrides the given key with the given value, every set item will be increased as usage. when the cache is full, least frequently used items will be evicted from linked list\n\n```go\ntype LFUNoTS struct {\n\t// contains filtered or unexported fields\n}\n```\nLFUNoTS holds the cache struct\n\n```go\nfunc (l *LFUNoTS) Delete(key string) error\n```\nDelete deletes the key and its dependencies\n\n```go\nfunc (l *LFUNoTS) Get(key string) (interface{}, error)\n```\nGet gets value of cache item then increments the usage of the item\n\n```go\nfunc (l *LFUNoTS) Set(key string, value interface{}) error\n```\nSet sets a new key-value pair Set increments the key usage count too\n\neg: cache.Set(\"test\\_key\",\"2\") cache.Set(\"test\\_key\",\"1\") if you try to set a value into same key its usage count will be increased and usage count of \"test\\_key\" will be 2 in this example\n\n```go\ntype LRU struct {\n\t// Mutex is used for handling the concurrent\n\t// read/write requests for cache\n\tsync.Mutex\n\t// contains filtered or unexported fields\n}\n```\nLRU Discards the least recently used items first. This algorithm requires keeping track of what was used when.\n\n```go\nfunc (l *LRU) Delete(key string) error\n```\nDelete deletes the given key-value pair from cache, this function doesnt return an error if item is not in the cache\n\n```go\nfunc (l *LRU) Get(key string) (interface{}, error)\n```\nGet returns the value of a given key if it exists, every get item will be moved to the head of the linked list for keeping track of least recent used item\n\n```go\nfunc (l *LRU) Set(key string, val interface{}) error\n```\nSet sets or overrides the given key with the given value, every set item will be moved or prepended to the head of the linked list for keeping track of least recent used item. When the cache is full, last item of the linked list will be evicted from the cache\n\n```go\ntype LRUNoTS struct {\n\t// contains filtered or unexported fields\n}\n```\nLRUNoTS Discards the least recently used items first. This algorithm requires keeping track of what was used when.\n\n```go\nfunc (l *LRUNoTS) Delete(key string) error\n```\nDelete deletes the given key-value pair from cache, this function doesnt return an error if item is not in the cache\n\n```go\nfunc (l *LRUNoTS) Get(key string) (interface{}, error)\n```\nGet returns the value of a given key if it exists, every get item will be moved to the head of the linked list for keeping track of least recent used item\n\n```go\nfunc (l *LRUNoTS) Set(key string, val interface{}) error\n```\nSet sets or overrides the given key with the given value, every set item will be moved or prepended to the head of the linked list for keeping track of least recent used item. When the cache is full, last item of the linked list will be evicted from the cache\n\n```go\ntype Memory struct {\n\t// Mutex is used for handling the concurrent\n\t// read/write requests for cache\n\tsync.Mutex\n\t// contains filtered or unexported fields\n}\n```\nMemory provides an inmemory caching mechanism\n\n```go\nfunc (r *Memory) Delete(key string) error\n```\nDelete deletes the given key-value pair from cache, this function doesnt return an error if item is not in the cache\n\n```go\nfunc (r *Memory) Get(key string) (interface{}, error)\n```\nGet returns the value of a given key if it exists\n\n```go\nfunc (r *Memory) Set(key string, value interface{}) error\n```\nSet sets a value to the cache or overrides existing one with the given value\n\n```go\ntype MemoryNoTS struct {\n\t// contains filtered or unexported fields\n}\n```\nMemoryNoTS provides a non-thread safe caching mechanism\n\n```go\nfunc NewMemoryNoTS() *MemoryNoTS\n```\nNewMemoryNoTS creates MemoryNoTS struct\n\n```go\nfunc (r *MemoryNoTS) Delete(key string) error\n```\nDelete deletes a given key, it doesnt return error if the item is not in the system\n\n```go\nfunc (r *MemoryNoTS) Get(key string) (interface{}, error)\n```\nGet returns a value of a given key if it exists and valid for the time being\n\n```go\nfunc (r *MemoryNoTS) Set(key string, value interface{}) error\n```\nSet will persist a value to the cache or override existing one with the new one\n\n```go\ntype MemoryTTL struct {\n\t// Mutex is used for handling the concurrent\n\t// read/write requests for cache\n\tsync.RWMutex\n\t// contains filtered or unexported fields\n}\n```\nMemoryTTL holds the required variables to compose an in memory cache system which also provides expiring key mechanism\n\n```go\nfunc NewMemoryWithTTL(ttl time.Duration) *MemoryTTL\n```\nNewMemoryWithTTL 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\n\n```go\nfunc (r *MemoryTTL) Delete(key string) error\n```\nDelete deletes a given key if exists\n\n```go\nfunc (r *MemoryTTL) Get(key string) (interface{}, error)\n```\nGet returns a value of a given key if it exists and valid for the time being\n\n```go\nfunc (r *MemoryTTL) Set(key string, value interface{}) error\n```\nSet will persist a value to the cache or override existing one with the new one\n\n```go\nfunc (r *MemoryTTL) StartGC(gcInterval time.Duration)\n```\nStartGC starts the garbage collection process in a go routine\n\n```go\nfunc (r *MemoryTTL) StopGC()\n```\nStopGC stops sweeping goroutine.\n\n```go\ntype MongoCache struct {\n\n\t// CollectionName speficies the optional collection name for mongoDB\n\t// if CollectionName is not set, then default value will be set\n\tCollectionName string\n\n\t// ttl is a duration for a cache key to expire\n\tTTL time.Duration\n\n\t// GCInterval specifies the time duration for garbage collector time interval\n\tGCInterval time.Duration\n\n\t// GCStart starts the garbage collector and deletes the\n\t// expired keys from mongo with given time interval\n\tGCStart bool\n\n\t// Mutex is used for handling the concurrent\n\t// read/write requests for cache\n\tsync.RWMutex\n\t// contains filtered or unexported fields\n}\n```\nMongoCache holds the cache values that will be stored in mongoDB\n\n```go\nfunc NewMongoCacheWithTTL(session *mgo.Session, configs ...Option) *MongoCache\n```\nNewMongoCacheWithTTL creates a caching layer backed by mongo. TTL's are managed either by a background cleaner or document is removed on the Get operation. Mongo TTL indexes are not utilized since there can be multiple systems using the same collection with different TTL values.\n\nThe responsibility of stopping the GC process belongs to the user.\n\nSession is not closed while stopping the GC.\n\nThis self-referential function satisfy you to avoid passing nil value to the function as parameter e.g (usage) : configure with defaults, just call; NewMongoCacheWithTTL(session)\n\nconfigure ttl duration with;\n\n\tNewMongoCacheWithTTL(session, func(m *MongoCache) {\n\t\t\tm.TTL = 2 * time.Minute\n\t})\n\nor NewMongoCacheWithTTL(session, SetTTL(time.Minute \\* 2))\n\nconfigure collection name with;\n\n\tNewMongoCacheWithTTL(session, func(m *MongoCache) {\n\t\t\tm.CollectionName = \"MongoCacheCollectionName\"\n\t})\n\n```go\nfunc (m *MongoCache) Delete(key string) error\n```\nDelete deletes a given key if exists\n\n```go\nfunc (m *MongoCache) EnsureIndex() error\n```\nEnsureIndex ensures the index with expireAt key\n\n```go\nfunc (m *MongoCache) Get(key string) (interface{}, error)\n```\nGet returns a value of a given key if it exists\n\n```go\nfunc (m *MongoCache) Set(key string, value interface{}) error\n```\nSet will persist a value to the cache or override existing one with the new one\n\n```go\nfunc (m *MongoCache) SetEx(key string, duration time.Duration, value interface{}) error\n```\nSetEx will persist a value to the cache or override existing one with the new one with ttl duration\n\n```go\nfunc (m *MongoCache) StartGC(gcInterval time.Duration)\n```\nStartGC starts the garbage collector with given time interval The expired data will be checked \u0026 deleted with given interval time\n\n```go\nfunc (m *MongoCache) StopGC()\n```\nStopGC stops sweeping goroutine.\n\n```go\ntype Option func(*MongoCache)\n```\nOption sets the options specified.\n\n```go\nfunc MustEnsureIndexExpireAt() Option\n```\nMustEnsureIndexExpireAt ensures the expireAt index usage: NewMongoCacheWithTTL(mongoSession, MustEnsureIndexExpireAt())\n\n```go\nfunc SetCollectionName(collName string) Option\n```\nSetCollectionName sets the collection name for mongoDB in MongoCache struct as option usage: NewMongoCacheWithTTL(mongoSession, SetCollectionName(\"mongoCollName\"))\n\n```go\nfunc SetGCInterval(duration time.Duration) Option\n```\nSetGCInterval sets the garbage collector interval in MongoCache struct as option usage: NewMongoCacheWithTTL(mongoSession, SetGCInterval(time\\*Minute))\n\n```go\nfunc SetTTL(duration time.Duration) Option\n```\nSetTTL sets the ttl duration in MongoCache as option usage: NewMongoCacheWithTTL(mongoSession, SetTTL(time\\*Minute))\n\n```go\nfunc StartGC() Option\n```\nStartGC enables the garbage collector in MongoCache struct usage: NewMongoCacheWithTTL(mongoSession, StartGC())\n\n```go\ntype ShardedCache interface {\n\t// Get returns single item from the backend if the requested item is not\n\t// found, returns NotFound err\n\tGet(shardID, key string) (interface{}, error)\n\n\t// Set sets a single item to the backend\n\tSet(shardID, key string, value interface{}) error\n\n\t// Delete deletes single item from backend\n\tDelete(shardID, key string) error\n\n\t// Deletes all items in that shard\n\tDeleteShard(shardID string) error\n}\n```\nShardedCache is the contract for all of the sharded cache backends that are supported by this package\n\n```go\ntype ShardedNoTS struct {\n\t// contains filtered or unexported fields\n}\n```\nShardedNoTS ; the concept behind this storage is that each cache entry is associated with a tenantID and this enables fast purging for just that tenantID\n\n```go\nfunc NewShardedNoTS(c func() Cache) *ShardedNoTS\n```\nNewShardedNoTS inits ShardedNoTS struct\n\n```go\nfunc (l *ShardedNoTS) Delete(tenantID, key string) error\n```\nDelete deletes a given key\n\n```go\nfunc (l *ShardedNoTS) DeleteShard(tenantID string) error\n```\nDeleteShard deletes the keys inside from maps of cache \u0026 itemCount\n\n```go\nfunc (l *ShardedNoTS) Get(tenantID, key string) (interface{}, error)\n```\nGet returns a value of a given key if it exists and valid for the time being\n\n```go\nfunc (l *ShardedNoTS) Set(tenantID, key string, val interface{}) error\n```\nSet will persist a value to the cache or override existing one with the new one\n\n```go\ntype ShardedTTL struct {\n\t// Mutex is used for handling the concurrent\n\t// read/write requests for cache\n\tsync.Mutex\n\t// contains filtered or unexported fields\n}\n```\nShardedTTL holds the required variables to compose an in memory sharded cache system which also provides expiring key mechanism\n\n```go\nfunc NewShardedCacheWithTTL(ttl time.Duration, f func() Cache) *ShardedTTL\n```\nNewShardedCacheWithTTL 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\n\n```go\nfunc NewShardedWithTTL(ttl time.Duration) *ShardedTTL\n```\nNewShardedWithTTL creates an in-memory sharded cache system ttl is used for expiration of a key from cache\n\n```go\nfunc (r *ShardedTTL) Delete(tenantID, key string) error\n```\nDelete deletes a given key if exists\n\n```go\nfunc (r *ShardedTTL) DeleteShard(tenantID string) error\n```\nDeleteShard deletes with given tenantID without key\n\n```go\nfunc (r *ShardedTTL) Get(tenantID, key string) (interface{}, error)\n```\nGet returns a value of a given key if it exists and valid for the time being\n\n```go\nfunc (r *ShardedTTL) Set(tenantID, key string, value interface{}) error\n```\nSet will persist a value to the cache or override existing one with the new one\n\n```go\nfunc (r *ShardedTTL) StartGC(gcInterval time.Duration)\n```\nStartGC starts the garbage collection process in a go routine\n\n","path":"github.com/koding/cache","name":"cache","synopsis":"Package cache provides basic caching mechanisms for Go(lang) projects.","isRedistributable":true}
