store

package
v1.5.11 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2020 License: MIT Imports: 12 Imported by: 3

Documentation

Overview

Package store provides different cache mechanisms and algorithms, To caches the authentication decisions.

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func NewFIFO(ctx context.Context, ttl time.Duration) *FIFO

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

func (*FIFO) Delete added in v1.2.0

func (f *FIFO) Delete(key string, _ *http.Request) error

Delete the value for a key.

func (*FIFO) Keys added in v1.2.2

func (f *FIFO) Keys() []string

Keys return cache records keys.

func (*FIFO) Load added in v1.2.0

func (f *FIFO) Load(key string, _ *http.Request) (interface{}, bool, error)

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 (*FIFO) Store added in v1.2.0

func (f *FIFO) Store(key string, value interface{}, _ *http.Request) error

Store sets the value for a key.

func (*FIFO) Update added in v1.5.9

func (f *FIFO) Update(key string, value interface{}, _ *http.Request) error

Update the value for a key without updating TTL.

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

func NewFileSystem(ctx context.Context, ttl time.Duration, path string) *FileSystem

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.

func (*FileSystem) Load added in v1.2.2

func (f *FileSystem) Load(key string, _ *http.Request) (interface{}, bool, error)

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 (*FileSystem) Store added in v1.2.2

func (f *FileSystem) Store(key string, value interface{}, _ *http.Request) error

Store sets the value for a key.

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

func New(maxEntries int) *LRU

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) Delete

func (l *LRU) Delete(key string, _ *http.Request) error

Delete the value for a key.

func (*LRU) Keys added in v1.2.2

func (l *LRU) Keys() []string

Keys return cache records keys.

func (*LRU) Len added in v1.2.0

func (l *LRU) Len() int

Len returns the number of items in the cache.

func (*LRU) Load

func (l *LRU) Load(key string, _ *http.Request) (interface{}, bool, error)

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) Peek added in v1.5.11

func (l *LRU) Peek(key string, _ *http.Request) (interface{}, bool, error)

Peek returns the value stored in the Cache for a key without updating the "recently used", 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.

func (*LRU) Store

func (l *LRU) Store(key string, value interface{}, _ *http.Request) error

Store sets the value for a key.

func (*LRU) Update added in v1.5.11

func (l *LRU) Update(key string, value interface{}, _ *http.Request) error

Update the value for a key without updating the "recently used".

type NoCache added in v1.5.4

type NoCache struct{}

NoCache is an implementation of Cache interface that never finds/stores a record.

func (NoCache) Delete added in v1.5.4

func (NoCache) Delete(_ string, _ *http.Request) (err error)

func (NoCache) Keys added in v1.5.4

func (NoCache) Keys() (keys []string)

func (NoCache) Load added in v1.5.4

func (NoCache) Load(_ string, _ *http.Request) (v interface{}, ok bool, err error)

func (NoCache) Store added in v1.5.4

func (NoCache) Store(_ string, _ interface{}, _ *http.Request) (err error)

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

type Replicator struct {
	InMemory   Cache
	Persistent Cache
}

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.

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

func (r *Replicator) Load(key string, req *http.Request) (interface{}, bool, error)

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.

Jump to

Keyboard shortcuts

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