cache

package
v3.24.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2020 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss means that a Get failed because the item wasn't present.
	ErrCacheMiss = errors.New("cache: miss")

	// ErrNotStored means the conditional write (Add or Replace) failed because
	// the condition was not met.
	ErrNotStored = errors.New("cache: not stored")

	// Null is the null Cache instance.
	Null = &nullCache{}
)

Functions

func Add

func Add(ctx context.Context, key string, value interface{}, expire time.Duration) error

Add sets the item in the cache, but only if the key does not already exist.

func Dec

func Dec(ctx context.Context, key string, value uint64) (int64, error)

Dec decrements a key by the value.

func Delete

func Delete(ctx context.Context, key string) error

Delete deletes the item with the given key.

func Inc

func Inc(ctx context.Context, key string, value uint64) (int64, error)

Inc increments a key by the value.

func Replace

func Replace(ctx context.Context, key string, value interface{}, expire time.Duration) error

Replace sets the item in the cache, but only if the key already exists.

func Set

func Set(ctx context.Context, key string, value interface{}, expire time.Duration) error

Set sets the item in the cache.

func WithCache

func WithCache(ctx context.Context, cache Cache) context.Context

WithCache sets Cache in the context.

Types

type Cache

type Cache interface {
	// Get gets the item for the given key.
	Get(key string) *Item

	// GetMulti gets the items for the given keys.
	GetMulti(keys ...string) ([]*Item, error)

	// Set sets the item in the cache.
	Set(key string, value interface{}, expire time.Duration) error

	// Add sets the item in the cache, but only if the key does not already exist.
	Add(key string, value interface{}, expire time.Duration) error

	// Replace sets the item in the cache, but only if the key already exists.
	Replace(key string, value interface{}, expire time.Duration) error

	// Delete deletes the item with the given key.
	Delete(key string) error

	// Inc increments a key by the value.
	Inc(key string, value uint64) (int64, error)

	// Dec decrements a key by the value.
	Dec(key string, value uint64) (int64, error)
}

Cache represents a cache instance.

func FromContext

func FromContext(ctx context.Context) (Cache, bool)

FromContext returns the instance of Cache in the context.

func NewMemcache

func NewMemcache(uri string, opts ...MemcacheOptionsFunc) Cache

NewMemcache create a new Memcache cache instance.

func NewRedis

func NewRedis(uri string, opts ...RedisOptionsFunc) (Cache, error)

NewRedis create a new Redis cache instance.

func NewRedisUniversal

func NewRedisUniversal(addrs []string, opts ...RedisOptionsFunc) (Cache, error)

type Item

type Item struct {
	// contains filtered or unexported fields
}

Item represents an item to be returned or stored in the cache

func Get

func Get(ctx context.Context, key string) *Item

Get gets the item for the given key.

func GetMulti

func GetMulti(ctx context.Context, keys ...string) ([]*Item, error)

GetMulti gets the items for the given keys.

func (Item) Bool

func (i Item) Bool() (bool, error)

Bool gets the cache items value as a bool, or and error.

func (Item) Bytes

func (i Item) Bytes() ([]byte, error)

Bytes gets the cache items value as bytes.

func (Item) Err

func (i Item) Err() error

Err returns the item error or nil.

func (Item) Float64

func (i Item) Float64() (float64, error)

Float64 gets the cache items value as a float64, or and error.

func (Item) Int64

func (i Item) Int64() (int64, error)

Int64 gets the cache items value as an int64, or and error.

func (Item) String

func (i Item) String() (string, error)

Bytes gets the cache items value as a string.

func (Item) Uint64

func (i Item) Uint64() (uint64, error)

Uint64 gets the cache items value as a uint64, or and error.

type MemcacheOptionsFunc

type MemcacheOptionsFunc func(*memcache.Client)

MemcacheOptionsFunc represents an configuration function for Memcache.

func WithIdleConns

func WithIdleConns(size int) MemcacheOptionsFunc

WithIdleConns configures the Memcache max idle connections.

func WithTimeout

func WithTimeout(timeout time.Duration) MemcacheOptionsFunc

WithTimeout configures the Memcache read and write timeout.

type RedisOptionsFunc

type RedisOptionsFunc func(*redis.UniversalOptions)

RedisOptionsFunc represents an configuration function for Redis.

func WithPoolSize

func WithPoolSize(size int) RedisOptionsFunc

WithPoolSize configures the Redis pool size.

func WithPoolTimeout

func WithPoolTimeout(timeout time.Duration) RedisOptionsFunc

WithPoolTimeout configures the Redis pool timeout.

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) RedisOptionsFunc

WithReadTimeout configures the Redis read timeout.

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) RedisOptionsFunc

WithWriteTimeout configures the Redis write timeout.

Jump to

Keyboard shortcuts

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