cache

package
v4.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("key not found in cache")
	ErrNoTTLSet = errors.New("key does not have a TTL set")
)

Common errors that might be returned by the cache package.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Prefix() string
	Set(key string, value string, expiration time.Duration) error
	Get(key string) (string, error)
	SetBool(key string, value bool, expiration time.Duration) error
	GetBool(key string) (bool, error)
	SetInt(key string, value int64, expiration time.Duration) error
	GetInt(key string) (int64, error)
	Incr(key string) (int64, error)
	SetJSON(key string, value interface{}, expiration time.Duration) error
	GetJSON(key string, result interface{}) error
	Del(key string) error
	Close() error
	TTL(key string) (time.Duration, error)
}

Cache defines basic cache operations including methods for setting and getting JSON objects.

type RedisClient

type RedisClient struct {
	Redis *redis.Client
	// contains filtered or unexported fields
}

RedisClient wraps the REDIS client to provide an implementation of the Cache interface. It allows defining a prefix that is applied to the key for all operations (optional).

func NewRedis

func NewRedis(redisHost string, redisPort string, prefix string) (*RedisClient, error)

NewRedis creates a new RedisClient.

func (*RedisClient) Close

func (r *RedisClient) Close() error

Close closes the connection to the REDIS server.

func (*RedisClient) Del

func (r *RedisClient) Del(key string) error

Del deletes a key value pair from REDIS. If the client was set up with a prefix it will be added in front of the key.

func (*RedisClient) Get

func (r *RedisClient) Get(key string) (string, error)

Get retrieves a value from REDIS. If the client was set up with a prefix it will be added in front of the key. If the value was not found ErrNotFound will be returned.

func (*RedisClient) GetBool

func (r *RedisClient) GetBool(key string) (bool, error)

GetBool retrieves a boolean value from REDIS. If the client was set up with a prefix it will be added in front of the key.

func (*RedisClient) GetInt

func (r *RedisClient) GetInt(key string) (int64, error)

GetInt retrieves an integer value from REDIS. If the client was set up with a prefix it will be added in front of the key.

func (*RedisClient) GetJSON

func (r *RedisClient) GetJSON(key string, result interface{}) error

GetJSON retrieves stringified JSON data from REDIS and parses it into the provided struct. If the client was set up with a prefix it will be added in front of the key.

func (*RedisClient) Incr

func (r *RedisClient) Incr(key string) (int64, error)

Incr increments a value in REDIS. If the client was set up with a prefix it will be added in front of the key. It returns the new (incremented) value.

func (*RedisClient) Prefix

func (r *RedisClient) Prefix() string

Prefix returns the prefix string that was defined for the REDIS client.

func (*RedisClient) Set

func (r *RedisClient) Set(key string, value string, expiration time.Duration) error

Set saves a key value pair to REDIS. If the client was set up with a prefix it will be added in front of the key. Redis `SET key value [expiration]` command. Use expiration for `SETEX`-like behavior. Zero expiration means the key has no expiration time.

func (*RedisClient) SetBool

func (r *RedisClient) SetBool(key string, value bool, expiration time.Duration) error

SetBool saves a boolean value to REDIS. If the client was set up with a prefix it will be added in front of the key. Zero expiration means the key has no expiration time.

func (*RedisClient) SetInt

func (r *RedisClient) SetInt(key string, value int64, expiration time.Duration) error

SetInt saves an integer value to REDIS. If the client was set up with a prefix it will be added in front of the key. Zero expiration means the key has no expiration time.

func (*RedisClient) SetJSON

func (r *RedisClient) SetJSON(key string, value interface{}, expiration time.Duration) error

SetJSON saves JSON data as string to REDIS. If the client was set up with a prefix it will be added in front of the key. Zero expiration means the key has no expiration time.

func (*RedisClient) TTL

func (r *RedisClient) TTL(key string) (time.Duration, error)

TTL returns remaining time to live of the given key found in REDIS. If the key doesn't exist, it returns ErrNotFound.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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