Documentation
¶
Index ¶
- type Client
- type RedisClient
- func (r *RedisClient) Close() error
- func (r *RedisClient) Delete(keys ...string) error
- func (r *RedisClient) Get(key string) (string, error)
- func (r *RedisClient) GetJSON(key string, target any) error
- func (r *RedisClient) Has(keys ...string) bool
- func (r *RedisClient) Lock(key, token string, ttl time.Duration) (bool, error)
- func (r *RedisClient) Set(key string, value string, ttl time.Duration) error
- func (r *RedisClient) SetJSON(key string, value any, ttl time.Duration) error
- func (r *RedisClient) Unlock(key, token string) error
- type RedisConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
Get(key string) (string, error)
Set(key string, value string, ttl time.Duration) error
SetJSON(key string, value any, ttl time.Duration) error
GetJSON(key string, target any) error
Lock(key, token string, ttl time.Duration) (bool, error)
Unlock(key, token string) error
Delete(keys ...string) error
Has(keys ...string) bool
Close() error
}
func NewRedis ¶
func NewRedis(cnf RedisConfig) (Client, error)
NewRedis initializes a new connection with the underlying go-redis and verifies connectivity.
Returns a (Client, nil) if connectivity test succeeds; otherwise (nil, error)
type RedisClient ¶
type RedisClient struct {
// contains filtered or unexported fields
}
RedisClient wraps the official go-redis client to provide streamlined helper methods.
func (*RedisClient) Close ¶
func (r *RedisClient) Close() error
Close gracefully closes the Redis client connection pool.
func (*RedisClient) Delete ¶
func (r *RedisClient) Delete(keys ...string) error
Delete removes one or multiple keys from Redis.
func (*RedisClient) Get ¶
func (r *RedisClient) Get(key string) (string, error)
Get retrieves a raw string value from Redis. Returns an error if the key does not exist.
func (*RedisClient) GetJSON ¶
func (r *RedisClient) GetJSON(key string, target any) error
GetJSON retrieves a JSON-byte-slice from Redis and unmarshals it into `target`.
`target` must be a pointer to the target interface
Returns error (redis communication error, key not exist or JSON unmarshal fails)
func (*RedisClient) Has ¶
func (r *RedisClient) Has(keys ...string) bool
Has checks if the key exists.
Returns false if key not found or error occurred; otherwise true.
func (*RedisClient) Lock ¶
Lock creates a specific key if not already set and sets the value to `token`.
`token` is more like a password for unlocking. Where if not exact match, unlock fails
Returns bool (lock granted), error (any error that occured)
func (*RedisClient) Set ¶
Set stores a raw string value with an optional Time-To-Live (TTL) expiration.
func (*RedisClient) SetJSON ¶
SetJSON serializes a Go struct into JSON and stores it in Redis.
`value` may either be a pointer or value
Returns error is json-marshal fails or redis encountered an error with the operation
func (*RedisClient) Unlock ¶
func (r *RedisClient) Unlock(key, token string) error
Unlock releases the lock on the specified key
Returns error if an error occurred or the unlock op fails (either token mismatch or non-existent key)
type RedisConfig ¶
type RedisConfig struct {
// Address is the address formated as host:port
Address string
// Password holds the password for authentication if enabled.
Password string
// Database is the database to be selected after connecting to the server.
Database int
Prefix string
ConnectTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
}