2015-01-07 12:15:13 +08:00
|
|
|
package redis_store
|
|
|
|
|
|
|
|
import (
|
|
|
|
redis "gopkg.in/redis.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RedisStore struct {
|
|
|
|
Client *redis.Client
|
|
|
|
}
|
|
|
|
|
2016-03-09 00:46:28 +08:00
|
|
|
func NewRedisStore(hostPort string, password string, database int) *RedisStore {
|
2015-01-07 12:15:13 +08:00
|
|
|
client := redis.NewTCPClient(&redis.Options{
|
|
|
|
Addr: hostPort,
|
2016-03-09 00:46:28 +08:00
|
|
|
Password: password,
|
2015-01-07 12:15:13 +08:00
|
|
|
DB: int64(database),
|
|
|
|
})
|
|
|
|
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 {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return fid, err
|
|
|
|
}
|
|
|
|
func (s *RedisStore) Put(fullFileName string, fid string) (err error) {
|
|
|
|
_, err = s.Client.Set(fullFileName, fid).Result()
|
|
|
|
if err == redis.Nil {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently the fid is not returned
|
2016-06-11 01:16:19 +08:00
|
|
|
func (s *RedisStore) Delete(fullFileName string) (err error) {
|
2015-01-07 12:15:13 +08:00
|
|
|
_, err = s.Client.Del(fullFileName).Result()
|
|
|
|
if err == redis.Nil {
|
|
|
|
err = nil
|
|
|
|
}
|
2016-06-11 01:16:19 +08:00
|
|
|
return err
|
2015-01-07 12:15:13 +08:00
|
|
|
}
|
|
|
|
|
2015-03-10 15:20:31 +08:00
|
|
|
func (s *RedisStore) Close() {
|
|
|
|
if s.Client != nil {
|
|
|
|
s.Client.Close()
|
2015-01-07 12:15:13 +08:00
|
|
|
}
|
|
|
|
}
|