cache

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2019 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("key not found in cache")

ErrNotFound is returned when the key was not found in the cache.

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)
	SetJSON(key string, value interface{}, expiration time.Duration) error
	GetJSON(key string, result interface{}) error
	Del(key string) error
	Close() error
}

Cache defines basic cache operartions 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) 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) 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) 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.

Jump to

Keyboard shortcuts

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