cache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: MIT Imports: 13 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 DefaultEncoding = GobEncoding
View Source
var NeverExpire time.Time

Functions

func NewGobEncoding

func NewGobEncoding() *gobEncoding

func NewJsonEncoding

func NewJsonEncoding() *jsonEncoding

func NewLiteralEncoding

func NewLiteralEncoding(fallback Encoding) *literalEncoding

NewLiteralEncoding is an encoding that will try its best to store the data as is, but fallback on another encoder if not possible.

func NewMemcacheClient

func NewMemcacheClient(c *memcache.Client, encoding Encoding) *memcacheClient
Example
memcacheClient := memcache.New("localhost:11211")
NewMemcacheClient(memcacheClient, DefaultEncoding)
Output:

func NewRedisClient

func NewRedisClient(c *redis.Client, encoding Encoding) *redisClient
Example
opts, err := redis.ParseURL("")
if err != nil {
	panic(err)
}
client := redis.NewClient(opts)
NewRedisClient(client, DefaultEncoding)
Output:

func TtlForExpiration

func TtlForExpiration(t time.Time) time.Duration

Types

type Client

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

	// Set writes the given item, unconditionally.
	Set(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(key string, data interface{}, expiration time.Time) error

	// Delete deletes the item with the provided key, if it exists.
	Delete(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(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(key string, delta uint64) (uint64, error)
}

Client is inspired from *memcached.Client

func NewMemoryClient

func NewMemoryClient() Client

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

type Decoder

type Decoder interface {
	Decode(b []byte, data interface{}) error
}

type Encoder

type Encoder interface {
	Encode(data interface{}) ([]byte, error)
}

type Encoding

type Encoding interface {
	Encoder
	Decoder
}
var GobEncoding Encoding = NewGobEncoding()
var JsonEncoding Encoding = NewJsonEncoding()

Jump to

Keyboard shortcuts

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