Documentation ¶
Overview ¶
Package store provides different cache mechanisms and algorithms, To caches the authentication decisions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCachedExp = errors.New("cache: Cached record have expired")
ErrCachedExp returned by cache when cached record have expired, and no longer living in cache (deleted)
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { // Load returns the value stored in the cache for a key, or nil if no value is present. // The ok result indicates whether value was found in the Cache. // The error reserved for moderate cache and returned if an error occurs, Otherwise nil. Load(key string, r *http.Request) (interface{}, bool, error) // Store sets the value for a key. // The error reserved for moderate cache and returned if an error occurs, Otherwise nil. Store(key string, value interface{}, r *http.Request) error // Delete deletes the value for a key. // The error reserved for moderate cache and returned if an error occurs, Otherwise nil. Delete(key string, r *http.Request) error }
Cache stores data so that future requests for that data can be served faster.
type FIFO ¶ added in v1.2.0
type FIFO struct { // TTL To expire a value in cache. // TTL Must be greater than 0. TTL time.Duration // OnEvicted optionally specifies a callback function to be // executed when an entry is purged from the cache. OnEvicted OnEvicted MU *sync.Mutex // contains filtered or unexported fields }
FIFO Cache instance safe for concurrent usage.
func NewFIFO ¶
NewFIFO return a simple FIFO Cache instance safe for concurrent usage, And spawning a garbage collector goroutine to collect expired record. The cache send record to garbage collector through a queue when it stored a new one. Once the garbage collector received the record it checks if record not expired to wait until expiration, Otherwise, wait for the next record. When the all expired record collected the garbage collector will be blocked, until new record stored to repeat the process. The context will be Passed to garbage collector
Example ¶
ctx, cancel := context.WithCancel(context.Background()) defer cancel() r, _ := http.NewRequest("GET", "/", nil) cache := NewFIFO(ctx, time.Minute*5) cache.Store("key", "value", r) v, ok, _ := cache.Load("key", r) fmt.Println(v, ok) cache.Delete("key", r) v, ok, _ = cache.Load("key", r) fmt.Println(v, ok)
Output: value true <nil> false
type LRU ¶
type LRU struct { // MaxEntries is the maximum number of cache entries before // an item is evicted. Zero means no limit. MaxEntries int // OnEvicted optionally specifies a callback function to be // executed when an entry is purged from the cache. OnEvicted OnEvicted // TTL To expire a value in cache. // 0 TTL means no expiry policy specified. TTL time.Duration MU *sync.Mutex // contains filtered or unexported fields }
LRU implements a fixed-size thread safe LRU cache. It is based on the LRU cache in Groupcache.
Example ¶
r, _ := http.NewRequest("GET", "/", nil) cache := New(2) cache.Store("key", "value", r) v, ok, _ := cache.Load("key", r) fmt.Println(v, ok) cache.Delete("key", r) v, ok, _ = cache.Load("key", r) fmt.Println(v, ok)
Output: value true <nil> false
func New ¶ added in v1.2.0
New creates a new LRU Cache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.
func (*LRU) Clear ¶ added in v1.2.0
func (l *LRU) Clear()
Clear purges all stored items from the cache.
func (*LRU) Load ¶
Load returns the value stored in the Cache for a key, or nil if no value is present. The ok result indicates whether value was found in the Cache.
func (*LRU) RemoveOldest ¶ added in v1.2.0
func (l *LRU) RemoveOldest()
RemoveOldest removes the oldest item from the cache.