cache

package module
v0.0.0-...-e0b3581 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2018 License: MIT Imports: 3 Imported by: 4

README

cache - In-memory cache for Go (golang) with TTL and LRU.

Go Report GolangCI Travis codecov License Maintenance GoDoc

cache provides a simple, goroutine safe, cache with a fixed number of entries. Each entry has a per-cache defined TTL. This TTL is reset on both modification and access of the value. As a result, if the cache is full, and no items have expired, when adding a new item, the item with the soonest expiration will be evicted.

It is based on the LRU implementation in golang-lru: github.com/hashicorp/golang-lru

Which in turn is based on the LRU implementation in groupcache: github.com/golang/groupcache/lru

Example

 import (
     "github.com/apibillme/cache"
 )

l := cache.New(128, cache.WithTTL(2*time.Second)) // create new cache with ttl and fixed size of 128 keys
l.Set(1, 1) // set key
l.Get(1) // get key
l.Del(1) // delete key

For more methods read the GoDoc.

Benchmark

As a loop is used based on the number of elements with setting unique keys O(n) - setting a value in the cache regardless of eviction (benchmark set to 128 unique) seems O(1).

BenchmarkSet100-4   	   10000	    101014 ns/op
BenchmarkSet200-4   	   10000	    190866 ns/op
BenchmarkSet400-4   	    5000	    373837 ns/op
BenchmarkSet800-4   	    2000	    784841 ns/op

Documentation

Overview

Package cache provides a simple, goroutine safe, cache with a fixed number of entries. Each entry has a per-cache defined TTL. This TTL is reset on both modification and access to the value. As a result, if the cache is full, and no items have expired, when adding a new item, the item with the soonest expiration will be evicted.

It is based on the LRU implementation in golang-lru: github.com/hashicorp/golang-lru

Which in turn is based on the LRU implementation in groupcache: github.com/golang/groupcache/lru

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Set a key with value to the cache. Returns true if an item was
	// evicted.
	Set(key, value interface{}) bool

	// Get an item from the cache by key. Returns the value if it exists,
	// and a bool stating whether or not it existed.
	Get(key interface{}) (interface{}, bool)

	// Keys returns a slice of all the keys in the cache
	Keys() []interface{}

	// Len returns the number of items present in the cache
	Len() int

	// Cap returns the total number of items the cache can retain
	Cap() int

	// Purge removes all items from the cache
	Purge()

	// Del deletes an item from the cache by key. Returns if an item was
	// actually deleted.
	Del(key interface{}) bool
}

func New

func New(cap int, opts ...Option) Cache

New creates a new Cache with cap entries that expire after ttl has elapsed since the item was added, modified or accessed.

type Option

type Option func(*cache)

func WithTTL

func WithTTL(val time.Duration) Option

func WithoutReset

func WithoutReset() Option

Jump to

Keyboard shortcuts

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