redis.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package utils
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "adm-ads/common.in/cache"
  7. )
  8. func RedisSet(tabname string, tail string, value interface{}) error {
  9. bytes, err := json.Marshal(value)
  10. if err != nil {
  11. return err
  12. }
  13. key := fmt.Sprintf("%s-%s", tabname, tail)
  14. _, err = cache.Redis.Set(key, string(bytes))
  15. return err
  16. }
  17. func RedisSetEx(key string, seconds int64, value interface{}) error {
  18. bytes, err := json.Marshal(value)
  19. if err != nil {
  20. return err
  21. }
  22. _, err = cache.Redis.SetEx(key, seconds, string(bytes))
  23. return err
  24. }
  25. func RedisGet(key string, value interface{}) error {
  26. ret, err := cache.Redis.Get(key)
  27. if err != nil {
  28. return err
  29. }
  30. if ret == "" {
  31. return errors.New("empty")
  32. }
  33. err = json.Unmarshal([]byte(ret), value)
  34. return err
  35. }
  36. func RedisDelKey(key string) error {
  37. _, err := cache.Redis.Del(key)
  38. return err
  39. }
  40. func RedisSadd(key string, members ...interface{}) (int64, error){
  41. return cache.Redis.SAdd(key,members...)
  42. }
  43. func RedisSIsmember(key string, member interface{}) (bool, error) {
  44. return cache.Redis.SIsmember(key,member)
  45. }
  46. func RedisRem(key string, members ...interface{}) (int64, error){
  47. return cache.Redis.SRem(key,members)
  48. }