2018-04-09 16:58:09 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis"
|
|
|
|
)
|
|
|
|
|
2019-09-10 14:22:21 +00:00
|
|
|
// RedisDBOptions configuration for RedisDB.
|
|
|
|
type RedisDBOptions struct {
|
|
|
|
Addr string `yaml:"Addr"`
|
|
|
|
Password string `yaml:"Password"`
|
|
|
|
DB int `yaml:"DB"`
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:58:09 +00:00
|
|
|
// RedisStore holds the client and maybe later some more metadata.
|
|
|
|
type RedisStore struct {
|
|
|
|
client *redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// RedisBatch simple batch implementation to satisfy the Store interface.
|
|
|
|
type RedisBatch struct {
|
|
|
|
mem map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len implements the Batch interface.
|
|
|
|
func (b *RedisBatch) Len() int {
|
|
|
|
return len(b.mem)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put implements the Batch interface.
|
|
|
|
func (b *RedisBatch) Put(k, v []byte) {
|
|
|
|
b.mem[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRedisBatch returns a new ready to use RedisBatch.
|
|
|
|
func NewRedisBatch() *RedisBatch {
|
|
|
|
return &RedisBatch{
|
|
|
|
mem: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:22:21 +00:00
|
|
|
// NewRedisStore returns an new initialized - ready to use RedisStore object.
|
|
|
|
func NewRedisStore(cfg RedisDBOptions) (*RedisStore, error) {
|
2018-04-09 16:58:09 +00:00
|
|
|
c := redis.NewClient(&redis.Options{
|
2019-09-10 14:22:21 +00:00
|
|
|
Addr: cfg.Addr,
|
|
|
|
Password: cfg.Password,
|
|
|
|
DB: cfg.DB,
|
2018-04-09 16:58:09 +00:00
|
|
|
})
|
|
|
|
if _, err := c.Ping().Result(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-10 14:22:21 +00:00
|
|
|
return &RedisStore{client: c}, nil
|
2018-04-09 16:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Batch implements the Store interface.
|
|
|
|
func (s *RedisStore) Batch() Batch {
|
|
|
|
return NewRedisBatch()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get implements the Store interface.
|
|
|
|
func (s *RedisStore) Get(k []byte) ([]byte, error) {
|
|
|
|
val, err := s.client.Get(string(k)).Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []byte(val), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put implements the Store interface.
|
|
|
|
func (s *RedisStore) Put(k, v []byte) error {
|
|
|
|
s.client.Set(string(k), string(v), 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutBatch implements the Store interface.
|
|
|
|
func (s *RedisStore) PutBatch(b Batch) error {
|
|
|
|
pipe := s.client.Pipeline()
|
|
|
|
for k, v := range b.(*RedisBatch).mem {
|
|
|
|
pipe.Set(k, v, 0)
|
|
|
|
}
|
|
|
|
_, err := pipe.Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek implements the Store interface.
|
|
|
|
func (s *RedisStore) Seek(k []byte, f func(k, v []byte)) {
|
|
|
|
iter := s.client.Scan(0, fmt.Sprintf("%s*", k), 0).Iterator()
|
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Val()
|
|
|
|
val, _ := s.client.Get(key).Result()
|
|
|
|
f([]byte(key), []byte(val))
|
|
|
|
}
|
|
|
|
}
|