cache

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2021 License: MIT Imports: 11 Imported by: 0

README

Cache

go-cache is a Go package which abstracts cache systems

Inspired from https://github.com/gookit/cache, but designed to be wrapped, similar to https://github.com/Shopify/go-storage.

Requirements

Installation

$ go get github.com/Shopify/go-cache

Usage

All caches in this package follow a simple interface.

If you want, you may wrap the Client to inject functionality, like a circuit breaker, logging, or metrics.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss   = errors.New("cache miss")
	ErrNotStored   = errors.New("not stored")
	ErrNotAPointer = errors.New("argument to Get() must be a pointer")
	ErrNotANumber  = errors.New("value currently stored is not a number")
)
View Source
var NeverExpire time.Time

Functions

This section is empty.

Types

type Client

type Client interface {
	// Get gets the item for the given key.
	// Returns ErrCacheMiss for a cache miss.
	// The key must be at most 250 bytes in length.
	Get(ctx context.Context, key string, data interface{}) error

	// Set writes the given item, unconditionally.
	Set(ctx context.Context, key string, data interface{}, expiration time.Time) error

	// Add writes the given item, if no value already exists for its
	// key. ErrNotStored is returned if that condition is not met.
	Add(ctx context.Context, key string, data interface{}, expiration time.Time) error

	// Delete deletes the item with the provided key, if it exists.
	Delete(ctx context.Context, key string) error

	// Increment adds delta to the currently stored number
	// If the key does not exist, it will be initialized to 0 with no expiration.
	// If the value overflows, it will loop around from 0
	// For many client implementations, you need to be using a LiteralEncoding for this feature to work
	Increment(ctx context.Context, key string, delta uint64) (uint64, error)

	// Decrement subtracts delta to the currently stored number
	// If the key does not exist, it will be initialized to 0 with no expiration, which will make it overflow.
	// If the value overflows, it will loop around from MaxUint64
	// For many client implementations, you need to be using a LiteralEncoding for this feature to work
	Decrement(ctx context.Context, key string, delta uint64) (uint64, error)
}

Client is inspired from *memcached.Client

func NewMemcacheClient

func NewMemcacheClient(c *memcache.Client, enc encoding.ValueEncoding) Client
Example
memcacheClient := memcache.New("localhost:11211")
NewMemcacheClient(memcacheClient, encoding.NewValueEncoding(encoding.GobEncoding))
Output:

func NewMemoryClient

func NewMemoryClient() Client

NewMemoryClient returns a Client that only stores in memory. Useful for stubbing tests.

func NewPrefixClient added in v2.1.0

func NewPrefixClient(client Client, prefix string) Client

NewPrefixClient returns a Client that adds a prefix to all keys

func NewRedisClient

func NewRedisClient(c *redis.Client, enc encoding.ValueEncoding) Client
Example
opts, err := redis.ParseURL("")
if err != nil {
	panic(err)
}
client := redis.NewClient(opts)
NewRedisClient(client, encoding.NewValueEncoding(encoding.GobEncoding))
Output:

type Mock added in v2.2.0

type Mock struct {
	mock.Mock
}

func (*Mock) Add added in v2.2.0

func (m *Mock) Add(ctx context.Context, key string, data interface{}, expiration time.Time) error

func (*Mock) Decrement added in v2.2.0

func (m *Mock) Decrement(ctx context.Context, key string, delta uint64) (uint64, error)

func (*Mock) Delete added in v2.2.0

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

func (*Mock) Get added in v2.2.0

func (m *Mock) Get(ctx context.Context, key string, data interface{}) error

func (*Mock) Increment added in v2.2.0

func (m *Mock) Increment(ctx context.Context, key string, delta uint64) (uint64, error)

func (*Mock) Set added in v2.2.0

func (m *Mock) Set(ctx context.Context, key string, data interface{}, expiration time.Time) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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