cache

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 6 Imported by: 0

README

Cache

This package provides an interface to interact with different cache stores, like BigCache and Redis. It allows you to perform common cache operations like Get/Set/Delete and Clear the entire cache.

Cache is optional, you can pass nil when building the equinox client and it will disable caching. The cache is enabled by default and uses BigCache.

The idea is to keep this package small and simple, providing only one internal cache and an external database, preferably Redis.

You can interact with the cache from the equinox client:

client, err := equinox.NewClient("RIOT_API_KEY")
body, err := client.Cache.Get("https://euw1.api.riotgames.com/...") // []byte, error

Stores

Documentation

Overview

Cache package to provide an interface to interact with cache stores.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheIsDisabled = errors.New("cache is disabled")
	ErrRedisOptionsNil = errors.New("redis options is nil")
)

Functions

This section is empty.

Types

type BigCacheClient

type BigCacheClient interface {
	Get(key string) ([]byte, error)
	Set(key string, entry []byte) error
	Delete(key string) error
	Reset() error
}

type BigCacheStore

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

func (*BigCacheStore) Clear

func (s *BigCacheStore) Clear(_ctx context.Context) error

func (*BigCacheStore) Delete

func (s *BigCacheStore) Delete(_ctx context.Context, key string) error

func (*BigCacheStore) Get

func (s *BigCacheStore) Get(_ctx context.Context, key string) ([]byte, error)

func (*BigCacheStore) Set

func (s *BigCacheStore) Set(_ctx context.Context, key string, value []byte) error

type Cache

type Cache struct {
	StoreType StoreType
	TTL       time.Duration
	Enabled   bool
	// contains filtered or unexported fields
}

func NewBigCache

func NewBigCache(ctx context.Context, config bigcache.Config) (*Cache, error)

Creates a new Cache using BigCache.

Requires a BigCache config that can be created with bigcache.DefaultConfig(n*time.Minute).

func NewRedis

func NewRedis(ctx context.Context, options *redis.Options, ttl time.Duration) (*Cache, error)

Creates a new Cache using go-redis.

func (*Cache) Clear

func (c *Cache) Clear(ctx context.Context) error

Clears the entire cache.

func (*Cache) Delete

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

Deletes an item from the cache.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string) ([]byte, error)

Returns an item from the cache. If no item is found, returns nil for the item and error.

func (Cache) MarshalZerologObject added in v1.0.0

func (c Cache) MarshalZerologObject(encoder *zerolog.Event)

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, item []byte) error

Saves an item under the key provided.

type RedisClient

type RedisClient interface {
	Get(ctx context.Context, key string) *redis.StringCmd
	Set(ctx context.Context, key string, value any, ttl time.Duration) *redis.StatusCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	FlushAll(ctx context.Context) *redis.StatusCmd
}

type RedisStore

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

func (*RedisStore) Clear

func (s *RedisStore) Clear(ctx context.Context) error

func (*RedisStore) Delete

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

func (*RedisStore) Get

func (s *RedisStore) Get(ctx context.Context, key string) ([]byte, error)

func (*RedisStore) Set

func (s *RedisStore) Set(ctx context.Context, key string, value []byte) error

type Store

type Store interface {
	Get(ctx context.Context, key string) ([]byte, error)
	Set(ctx context.Context, key string, value []byte) error
	Delete(ctx context.Context, key string) error
	Clear(ctx context.Context) error
}

type StoreType added in v0.19.0

type StoreType string
const (
	BigCache   StoreType = "BigCache"
	RedisCache StoreType = "Redis"
)

Jump to

Keyboard shortcuts

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