Documentation ¶
Index ¶
- Variables
- type Cache
- type RedisClient
- func (r *RedisClient) Close() error
- func (r *RedisClient) Del(key string) error
- func (r *RedisClient) Get(key string) (string, error)
- func (r *RedisClient) GetBool(key string) (bool, error)
- func (r *RedisClient) GetInt(key string) (int64, error)
- func (r *RedisClient) GetJSON(key string, result interface{}) error
- func (r *RedisClient) Incr(key string) (int64, error)
- func (r *RedisClient) Prefix() string
- func (r *RedisClient) Set(key string, value string, expiration time.Duration) error
- func (r *RedisClient) SetBool(key string, value bool, expiration time.Duration) error
- func (r *RedisClient) SetInt(key string, value int64, expiration time.Duration) error
- func (r *RedisClient) SetJSON(key string, value interface{}, expiration time.Duration) error
- func (r *RedisClient) TTL(key string) (time.Duration, error)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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.