cache

package
v0.0.0-...-a99687c Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: AGPL-3.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	CheckStatus(ctx context.Context) (time.Duration, error)
	Get(key string) ([]byte, bool)
	MultiGet(keys []string) [][]byte
	Keys(prefix string) []string
	Clear(key string)
	Set(key string, data []byte, expiration time.Duration)
	SetNX(key string, data []byte, expiration time.Duration)
	GetCompressed(key string) (io.Reader, bool)
	SetCompressed(key string, data []byte, expiration time.Duration)
	RefreshTTL(key string, expiration time.Duration)
}

Cache is a rudimentary key/value caching store backed by redis. It offers a Get/Set interface as well a its gzip compressed alternative GetCompressed/SetCompressed

func New

func New(client redis.UniversalClient) Cache

New instantiate a Cache Client.

The backend selection is done based on the `client` argument. If a client is given, the redis backend is chosen, if nil is provided the inmemory backend would be chosen.

type InMemory

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

InMemory implementation of the Cache client.

func NewInMemory

func NewInMemory() *InMemory

NewInMemory instantiates a new in-memory Cache Client.

func (*InMemory) CheckStatus

func (c *InMemory) CheckStatus(ctx context.Context) (time.Duration, error)

CheckStatus checks that the cache is ready, or returns an error.

func (*InMemory) Clear

func (c *InMemory) Clear(key string)

Clear removes a key from the cache

func (*InMemory) Get

func (c *InMemory) Get(key string) ([]byte, bool)

Get fetch the cached asset at the given key, and returns true only if the asset was found.

func (*InMemory) GetCompressed

func (c *InMemory) GetCompressed(key string) (io.Reader, bool)

GetCompressed works like Get but expect a compressed asset that is uncompressed.

func (*InMemory) Keys

func (c *InMemory) Keys(prefix string) []string

Keys returns the list of keys with the given prefix.

func (*InMemory) MultiGet

func (c *InMemory) MultiGet(keys []string) [][]byte

MultiGet can be used to fetch several keys at once.

func (*InMemory) RefreshTTL

func (c *InMemory) RefreshTTL(key string, expiration time.Duration)

RefreshTTL can be used to update the TTL of an existing entry in the cache.

func (*InMemory) Set

func (c *InMemory) Set(key string, data []byte, expiration time.Duration)

Set stores an asset to the given key.

func (*InMemory) SetCompressed

func (c *InMemory) SetCompressed(key string, data []byte, expiration time.Duration)

SetCompressed works like Set but compress the asset data before storing it.

func (*InMemory) SetNX

func (c *InMemory) SetNX(key string, data []byte, expiration time.Duration)

SetNX stores the data in the cache only if the key doesn't exist yet.

type Redis

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

Redis implementation of the cache client.

func NewRedis

func NewRedis(client redis.UniversalClient) *Redis

NewRedis instantiate a new Redis Cache Client.

func (*Redis) CheckStatus

func (c *Redis) CheckStatus(ctx context.Context) (time.Duration, error)

CheckStatus checks that the cache is ready, or returns an error.

func (*Redis) Clear

func (c *Redis) Clear(key string)

Clear removes a key from the cache

func (*Redis) Get

func (c *Redis) Get(key string) ([]byte, bool)

Get fetch the cached asset at the given key, and returns true only if the asset was found.

func (*Redis) GetCompressed

func (c *Redis) GetCompressed(key string) (io.Reader, bool)

GetCompressed works like Get but expect a compressed asset that is uncompressed.

func (*Redis) Keys

func (c *Redis) Keys(prefix string) []string

Keys returns the list of keys with the given prefix.

Note: it can be slow and should be used carefully.

func (*Redis) MultiGet

func (c *Redis) MultiGet(keys []string) [][]byte

MultiGet can be used to fetch several keys at once.

func (*Redis) RefreshTTL

func (c *Redis) RefreshTTL(key string, expiration time.Duration)

RefreshTTL can be used to update the TTL of an existing entry in the cache.

func (*Redis) Set

func (c *Redis) Set(key string, data []byte, expiration time.Duration)

Set stores an asset to the given key.

func (*Redis) SetCompressed

func (c *Redis) SetCompressed(key string, data []byte, expiration time.Duration)

SetCompressed works like Set but compress the asset data before storing it.

func (*Redis) SetNX

func (c *Redis) SetNX(key string, data []byte, expiration time.Duration)

SetNX stores the data in the cache only if the key doesn't exist yet.

Jump to

Keyboard shortcuts

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