redis.go 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * @description: redis 操作相关
  3. * @Author: CP
  4. * @Date: 2021-01-27 11:35:58
  5. * @FilePath: \construction_management\lib\redis.go
  6. */
  7. package lib
  8. import (
  9. "log"
  10. "strconv"
  11. "go.mod/comm"
  12. "go.mod/datasource"
  13. )
  14. type Redis struct {
  15. }
  16. func NewRedis() *Redis {
  17. return &Redis{}
  18. }
  19. // 从redis获得标段ID
  20. func (s *Redis) GetBidsectionIdByCache(key string) int {
  21. rds := datasource.InstanceCache()
  22. // 读取缓存
  23. rs, err := rds.Do("GET", key)
  24. if err != nil {
  25. log.Println("redis.GetBidsectionId GET key=", key, ", error=", err)
  26. return 0
  27. }
  28. str := comm.GetString(rs, "")
  29. num, err := strconv.Atoi(str)
  30. if err != nil {
  31. return 0
  32. } else {
  33. return int(num)
  34. }
  35. }
  36. // 设置标段ID的redis
  37. func (s *Redis) SetBidsectionIdByCache(key string, value interface{}) {
  38. // 集群模式,redis缓存
  39. rds := datasource.InstanceCache()
  40. // 更新缓存
  41. _, err := rds.Do("SET", key, value)
  42. if err != nil {
  43. log.Println("redis.SetBidsectionId SET key=", key,
  44. ", value=", value, ", error=", err)
  45. }
  46. }