2015-01-07 04:15:13 +00:00
|
|
|
package redis_store
|
|
|
|
|
|
|
|
import (
|
2016-09-08 03:35:54 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
|
2018-04-16 07:27:33 +00:00
|
|
|
"github.com/go-redis/redis"
|
2015-01-07 04:15:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RedisStore struct {
|
|
|
|
Client *redis.Client
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:46:28 +00:00
|
|
|
func NewRedisStore(hostPort string, password string, database int) *RedisStore {
|
2018-04-16 07:27:33 +00:00
|
|
|
client := redis.NewClient(&redis.Options{
|
2015-01-07 04:15:13 +00:00
|
|
|
Addr: hostPort,
|
2016-03-08 16:46:28 +00:00
|
|
|
Password: password,
|
2018-04-16 07:27:33 +00:00
|
|
|
DB: database,
|
2015-01-07 04:15:13 +00:00
|
|
|
})
|
|
|
|
return &RedisStore{Client: client}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RedisStore) Get(fullFileName string) (fid string, err error) {
|
|
|
|
fid, err = s.Client.Get(fullFileName).Result()
|
|
|
|
if err == redis.Nil {
|
2016-09-08 03:35:54 +00:00
|
|
|
err = filer.ErrNotFound
|
2015-01-07 04:15:13 +00:00
|
|
|
}
|
|
|
|
return fid, err
|
|
|
|
}
|
|
|
|
func (s *RedisStore) Put(fullFileName string, fid string) (err error) {
|
2018-04-16 07:27:33 +00:00
|
|
|
_, err = s.Client.Set(fullFileName, fid, 0).Result()
|
2015-01-07 04:15:13 +00:00
|
|
|
if err == redis.Nil {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently the fid is not returned
|
2016-06-10 17:16:19 +00:00
|
|
|
func (s *RedisStore) Delete(fullFileName string) (err error) {
|
2015-01-07 04:15:13 +00:00
|
|
|
_, err = s.Client.Del(fullFileName).Result()
|
|
|
|
if err == redis.Nil {
|
|
|
|
err = nil
|
|
|
|
}
|
2016-06-10 17:16:19 +00:00
|
|
|
return err
|
2015-01-07 04:15:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 07:20:31 +00:00
|
|
|
func (s *RedisStore) Close() {
|
|
|
|
if s.Client != nil {
|
|
|
|
s.Client.Close()
|
2015-01-07 04:15:13 +00:00
|
|
|
}
|
|
|
|
}
|