Documentation ¶
Index ¶
- Constants
- Variables
- func FetchOrSave(ctx context.Context, c Cache, key string, value interface{}, ...) error
- func Initialize(typ, addr string) error
- func NewContext(ctx context.Context, c Cache) context.Context
- func Register(typ string, factory func(opts Options) (Cache, error))
- type Cache
- type Codec
- type Iterator
- type Option
- type Options
Constants ¶
View Source
const ( // Memory the cache name of memory Memory = "memory" // Redis the cache name of redis Redis = "redis" // RedisSentinel the cache name of redis sentinel RedisSentinel = "redis+sentinel" )
Variables ¶
View Source
var ( // ErrNotFound error returns when the key value not found in the cache ErrNotFound = errors.New("key not found") )
Functions ¶
func FetchOrSave ¶
func FetchOrSave(ctx context.Context, c Cache, key string, value interface{}, builder func() (interface{}, error), expiration ...time.Duration) error
FetchOrSave retrieves the value for the key if present in the cache. Otherwise, it saves the value from the builder and retrieves the value for the key again.
func Initialize ¶
Initialize initialize the default cache from the addr
func NewContext ¶
NewContext returns new context with cache
Types ¶
type Cache ¶
type Cache interface { // Contains returns true if key exists Contains(ctx context.Context, key string) bool // Delete delete item from cache by key Delete(ctx context.Context, key string) error // Fetch retrieve the cached key value Fetch(ctx context.Context, key string, value interface{}) error // Ping ping the cache Ping(ctx context.Context) error // Save cache the value by key Save(ctx context.Context, key string, value interface{}, expiration ...time.Duration) error // Scan scans the keys matched by match string // NOTICE: memory cache does not support use wildcard, compared by strings.Contains Scan(ctx context.Context, match string) (Iterator, error) }
Cache cache interface
func FromContext ¶
FromContext returns cache from context
type Codec ¶
type Codec interface { // Encode returns the encoded byte array of v. Encode(v interface{}) ([]byte, error) // Decode analyzes the encoded data and stores the result into the v. Decode(data []byte, v interface{}) error }
Codec codec interface for cache
Click to show internal directories.
Click to hide internal directories.