Files
storage/redis/redis.go
Juan Calderon-Perez 3a8b8d4f71 Add Conn() support to all storage drivers. (#451)
* Add DB() support for Redis driver

* Added support for DB() to all drivers

* Fixed typo in README and Lint issue

* Fix lint issue with ristretto db

* Fix lint issue with bbolt db

* Rename DB() to Conn()

* Replace all instances of _DB with _Conn

* Update all the README files

* Return ArangoDB Client instead of DB
2022-08-15 07:58:13 +02:00

107 lines
2.0 KiB
Go

package redis
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis/v8"
)
// Storage interface that is implemented by storage providers
type Storage struct {
db *redis.Client
}
// New creates a new redis storage
func New(config ...Config) *Storage {
// Set default config
cfg := configDefault(config...)
// Create new redis client
var options *redis.Options
var err error
if cfg.URL != "" {
options, err = redis.ParseURL(cfg.URL)
options.TLSConfig = cfg.TLSConfig
if err != nil {
panic(err)
}
} else {
options = &redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
DB: cfg.Database,
Username: cfg.Username,
Password: cfg.Password,
TLSConfig: cfg.TLSConfig,
}
}
db := redis.NewClient(options)
// Test connection
if err := db.Ping(context.Background()).Err(); err != nil {
panic(err)
}
// Empty collection if Clear is true
if cfg.Reset {
if err := db.FlushDB(context.Background()).Err(); err != nil {
panic(err)
}
}
// Create new store
return &Storage{
db: db,
}
}
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, nil
}
val, err := s.db.Get(context.Background(), key).Bytes()
if err == redis.Nil {
return nil, nil
}
return val, err
}
// Set key with value
// Set key with value
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
// Ain't Nobody Got Time For That
if len(key) <= 0 || len(val) <= 0 {
return nil
}
return s.db.Set(context.Background(), key, val, exp).Err()
}
// Delete key by key
func (s *Storage) Delete(key string) error {
// Ain't Nobody Got Time For That
if len(key) <= 0 {
return nil
}
return s.db.Del(context.Background(), key).Err()
}
// Reset all keys
func (s *Storage) Reset() error {
return s.db.FlushDB(context.Background()).Err()
}
// Close the database
func (s *Storage) Close() error {
return s.db.Close()
}
// Return database client
func (s *Storage) Conn() *redis.Client {
return s.db
}