cache.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package reminder
  2. import (
  3. "fmt"
  4. "git.getensh.com/common/gopkgs/cache"
  5. )
  6. func getGardenInfoKey(timestamp int64) string {
  7. return fmt.Sprintf("propety-urge-garden-info-%d", timestamp)
  8. }
  9. func getObjUsersOpenidKey(timestamp, gardenId int64) string {
  10. return fmt.Sprintf("propety-urge-openid-%d-%d", timestamp, gardenId)
  11. }
  12. func getObjUsersPhoneKey(timestamp, gardenId int64) string {
  13. return fmt.Sprintf("propety-urge-phone-%d-%d", timestamp, gardenId)
  14. }
  15. func cacheGardenInfo(timestamp int64, info string) {
  16. key := getGardenInfoKey(timestamp)
  17. _, _ =cache.Redis().SetEx(key, 3600*6, info)
  18. }
  19. func getGardenInfo(timestamp int64) string {
  20. key := getGardenInfoKey(timestamp)
  21. str, _ := cache.Redis().Get(key)
  22. return str
  23. }
  24. func cacheAllObjUsersPhone(timestamp, gardenId int64, phones []interface{}) {
  25. key := getObjUsersPhoneKey(timestamp, gardenId)
  26. cache.Redis().SAdd(key, phones ...)
  27. }
  28. func cacheAllObjUsersOpenid(timestamp, gardenId int64, openIds []interface{}) {
  29. key := getObjUsersOpenidKey(timestamp, gardenId)
  30. cache.Redis().SAdd(key, openIds ...)
  31. }
  32. func popAllObjUsersPhones(timestamp, gardenId int64) []string {
  33. key := getObjUsersPhoneKey(timestamp, gardenId)
  34. ret, _ := cache.Redis().SMembers(key)
  35. _,_ = cache.Redis().Del(key)
  36. return ret
  37. }
  38. func popAllObjUsersOpenids(timestamp, gardenId int64) []string {
  39. key := getObjUsersOpenidKey(timestamp, gardenId)
  40. ret, _ := cache.Redis().SMembers(key)
  41. _,_ = cache.Redis().Del(key)
  42. return ret
  43. }
  44. func delOldObjUsers(timestamp, gardenId int64) {
  45. count := 1
  46. // 清空之前7天的缓存,不管有没有
  47. for count < 8 {
  48. timestamp -= 3600*24
  49. key := getObjUsersPhoneKey(timestamp, gardenId)
  50. _, _ = cache.Redis().Del(key)
  51. key = getObjUsersOpenidKey(timestamp, gardenId)
  52. _, _ = cache.Redis().Del(key)
  53. count++
  54. }
  55. }