Documentation ¶
Overview ¶
Package store provides different cache mechanisms and algorithms, To caches the authentication decisions.
Index ¶
- Variables
- type Cache
- type FIFO
- type FileSystem
- type LRU
- func (l *LRU) Clear()
- func (l *LRU) Delete(key string, _ *http.Request) error
- func (l *LRU) Keys() []string
- func (l *LRU) Len() int
- func (l *LRU) Load(key string, _ *http.Request) (interface{}, bool, error)
- func (l *LRU) RemoveOldest()
- func (l *LRU) Store(key string, value interface{}, _ *http.Request) error
- type OnEvicted
- type Replicator
- func (r *Replicator) Delete(key string, req *http.Request) error
- func (r *Replicator) IsSynced() bool
- func (r *Replicator) Keys() []string
- func (r *Replicator) Load(key string, req *http.Request) (interface{}, bool, error)
- func (r *Replicator) Store(key string, value interface{}, req *http.Request) error
- func (r *Replicator) Sync() error
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 // Keys return cache records keys. Keys() []string }
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 FileSystem ¶ added in v1.2.2
type FileSystem struct { // TTL To expire a value in cache. // 0 TTL means no expiry policy specified. TTL time.Duration MU *sync.RWMutex // contains filtered or unexported fields }
FileSystem stores cache record in the filesystem. FileSystem encode/decode cache record using encoding/gob.
func NewFileSystem ¶ added in v1.2.2
NewFileSystem return FileSystem 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
func (*FileSystem) Delete ¶ added in v1.2.2
func (f *FileSystem) Delete(key string, _ *http.Request) error
Delete the value for a key.
func (*FileSystem) Keys ¶ added in v1.2.2
func (f *FileSystem) Keys() []string
Keys return cache records keys.
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.
type OnEvicted ¶ added in v1.2.0
type OnEvicted func(key string, value interface{})
OnEvicted define a function signature to be executed when an entry is purged from the cache.
type Replicator ¶ added in v1.2.2
Replicator holding two caches instances to replicate data between them on load and store data, Replicator is typically used to replicate data between in-memory and a persistent caches, to obtain data consistency and persistency between runs of a program or scaling purposes. Most users will use distributed caching instead.
NOTICE: Replicator cache ignore errors from in-memory cache since all data first stored in Persistent.
Example ¶
lru := New(20) fileSystem := NewFileSystem(context.TODO(), 0, "/tmp") replicator := Replicator{ Persistent: fileSystem, InMemory: lru, } replicator.Store("key", "value", nil) v, _, _ := lru.Load("key", nil) fmt.Println(v) v, _, _ = fileSystem.Load("key", nil) fmt.Println(v)
Output: value value
func (*Replicator) Delete ¶ added in v1.2.2
func (r *Replicator) Delete(key string, req *http.Request) error
Delete the value for a key.
func (*Replicator) IsSynced ¶ added in v1.2.2
func (r *Replicator) IsSynced() bool
IsSynced return true/false if two cached keys are equal.
func (*Replicator) Keys ¶ added in v1.2.2
func (r *Replicator) Keys() []string
Keys return cache records keys.
func (*Replicator) Load ¶ added in v1.2.2
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. When any error occurs fallback to persistent cache.
func (*Replicator) Store ¶ added in v1.2.2
func (r *Replicator) Store(key string, value interface{}, req *http.Request) error
Store sets the value for a key.
func (*Replicator) Sync ¶ added in v1.2.2
func (r *Replicator) Sync() error
Sync two caches by reload all persistent cache records into in-memory cache.