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
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-09-26 16:05:20 +00:00
|
|
|
return newMemoryBatch()
|
2018-04-09 16:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get implements the Store interface.
|
|
|
|
func (s *RedisStore) Get(k []byte) ([]byte, error) {
|
|
|
|
val, err := s.client.Get(string(k)).Result()
|
|
|
|
if err != nil {
|
2019-09-27 12:40:44 +00:00
|
|
|
if err == redis.Nil {
|
|
|
|
err = ErrKeyNotFound
|
|
|
|
}
|
2018-04-09 16:58:09 +00:00
|
|
|
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()
|
2019-09-26 16:05:20 +00:00
|
|
|
for k, v := range b.(*MemoryBatch).m {
|
2019-10-07 14:05:53 +00:00
|
|
|
pipe.Set(k, v, 0)
|
2018-04-09 16:58:09 +00:00
|
|
|
}
|
|
|
|
_, 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))
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 15:52:47 +00:00
|
|
|
|
|
|
|
// Close implements the Store interface.
|
|
|
|
func (s *RedisStore) Close() error {
|
|
|
|
return s.client.Close()
|
|
|
|
}
|