redis_store.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package redis_store
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/filer"
  4. "github.com/go-redis/redis"
  5. )
  6. type RedisStore struct {
  7. Client *redis.Client
  8. }
  9. func NewRedisStore(hostPort string, password string, database int) *RedisStore {
  10. client := redis.NewClient(&redis.Options{
  11. Addr: hostPort,
  12. Password: password,
  13. DB: database,
  14. })
  15. return &RedisStore{Client: client}
  16. }
  17. func (s *RedisStore) Get(fullFileName string) (fid string, err error) {
  18. fid, err = s.Client.Get(fullFileName).Result()
  19. if err == redis.Nil {
  20. err = filer.ErrNotFound
  21. }
  22. return fid, err
  23. }
  24. func (s *RedisStore) Put(fullFileName string, fid string) (err error) {
  25. _, err = s.Client.Set(fullFileName, fid, 0).Result()
  26. if err == redis.Nil {
  27. err = nil
  28. }
  29. return err
  30. }
  31. // Currently the fid is not returned
  32. func (s *RedisStore) Delete(fullFileName string) (err error) {
  33. _, err = s.Client.Del(fullFileName).Result()
  34. if err == redis.Nil {
  35. err = nil
  36. }
  37. return err
  38. }
  39. func (s *RedisStore) Close() {
  40. if s.Client != nil {
  41. s.Client.Close()
  42. }
  43. }