cache.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Author: simon (ynwdlxm@163.com)
  2. // Date: 2025/7/21 10:10
  3. // Desc: 缓存包
  4. package cache
  5. import (
  6. "encoding/json"
  7. "sync"
  8. "time"
  9. "github.com/spf13/cast"
  10. "github.com/runningwater/gohub/pkg/logger"
  11. )
  12. type Service struct {
  13. Store Store
  14. }
  15. var once sync.Once
  16. var Cache *Service
  17. func InitWithCacheStore(store Store) {
  18. once.Do(func() {
  19. Cache = &Service{
  20. Store: store,
  21. }
  22. })
  23. }
  24. func Set(key string, value any, expire time.Duration) {
  25. bytes, err := json.Marshal(value)
  26. logger.LogIf(err)
  27. Cache.Store.Set(key, string(bytes), expire)
  28. }
  29. func Get(key string) any {
  30. stringValue := Cache.Store.Get(key)
  31. var wanted any
  32. err := json.Unmarshal([]byte(stringValue), &wanted)
  33. logger.LogIf(err)
  34. return wanted
  35. }
  36. func Has(key string) bool {
  37. return Cache.Store.Has(key)
  38. }
  39. // GetObject 获取对象,用法如下:
  40. //
  41. // model := user.User{}
  42. // cache.GetObject("user:1", &model)
  43. func GetObject(key string, wanted any) {
  44. val := Cache.Store.Get(key)
  45. if len(val) > 0 {
  46. err := json.Unmarshal([]byte(val), &wanted)
  47. logger.LogIf(err)
  48. } else {
  49. logger.WarnString("cache", "GetObject", "cache key ["+key+"] not found")
  50. }
  51. }
  52. func GetString(key string) string {
  53. return cast.ToString(Get(key))
  54. }
  55. func GetBool(key string) bool {
  56. return cast.ToBool(Get(key))
  57. }
  58. func GetInt(key string) int {
  59. return cast.ToInt(Get(key))
  60. }
  61. func GetInt32(key string) int32 {
  62. return cast.ToInt32(Get(key))
  63. }
  64. func GetInt64(key string) int64 {
  65. return cast.ToInt64(Get(key))
  66. }
  67. func GetUint(key string) uint {
  68. return cast.ToUint(Get(key))
  69. }
  70. func GetUint32(key string) uint32 {
  71. return cast.ToUint32(Get(key))
  72. }
  73. func GetUint64(key string) uint64 {
  74. return cast.ToUint64(Get(key))
  75. }
  76. func GetFloat32(key string) float32 {
  77. return cast.ToFloat32(Get(key))
  78. }
  79. func GetFloat64(key string) float64 {
  80. return cast.ToFloat64(Get(key))
  81. }
  82. func GetTime(key string) time.Time {
  83. return cast.ToTime(Get(key))
  84. }
  85. func GetDuration(key string) time.Duration {
  86. return cast.ToDuration(Get(key))
  87. }
  88. func GetIntSlice(key string) []int {
  89. return cast.ToIntSlice(Get(key))
  90. }
  91. func GetStringSlice(key string) []string {
  92. return cast.ToStringSlice(Get(key))
  93. }
  94. func GetStringMap(key string) map[string]string {
  95. return cast.ToStringMapString(Get(key))
  96. }
  97. func GetStringMapString(key string) map[string]string {
  98. return cast.ToStringMapString(Get(key))
  99. }
  100. func GetStringMapStringSlice(key string) map[string][]string {
  101. return cast.ToStringMapStringSlice(Get(key))
  102. }
  103. func GetStringMapInt(key string) map[string]int {
  104. return cast.ToStringMapInt(Get(key))
  105. }
  106. func Forget(key string) {
  107. Cache.Store.Forget(key)
  108. }
  109. func Forever(key string, value string) {
  110. Cache.Store.Set(key, value, 0)
  111. }
  112. func Flush() {
  113. Cache.Store.Flush()
  114. }
  115. func Increment(params ...any) {
  116. Cache.Store.Increment(params...)
  117. }
  118. func Decrement(params ...any) {
  119. Cache.Store.Decrement(params...)
  120. }
  121. func IsAlive() error {
  122. return Cache.Store.IsAlive()
  123. }