store

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: MIT Imports: 6 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
}

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

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

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.

Jump to

Keyboard shortcuts

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