memory.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package cache
  2. import (
  3. "fmt"
  4. "strconv"
  5. "sync"
  6. "time"
  7. "github.com/spf13/cast"
  8. )
  9. type item struct {
  10. Value string
  11. Expired time.Time
  12. }
  13. // NewMemory memory模式
  14. func NewMemory() *Memory {
  15. return &Memory{
  16. items: new(sync.Map),
  17. }
  18. }
  19. type Memory struct {
  20. items *sync.Map
  21. mutex sync.RWMutex
  22. }
  23. func (*Memory) String() string {
  24. return "memory"
  25. }
  26. func (m *Memory) connect() {
  27. }
  28. func (m *Memory) Get(key string) (string, error) {
  29. item, err := m.getItem(key)
  30. if err != nil || item == nil {
  31. return "", err
  32. }
  33. return item.Value, nil
  34. }
  35. func (m *Memory) getItem(key string) (*item, error) {
  36. var err error
  37. i, ok := m.items.Load(key)
  38. if !ok {
  39. return nil, nil
  40. }
  41. switch i.(type) {
  42. case *item:
  43. item := i.(*item)
  44. if item.Expired.Before(time.Now()) {
  45. //过期
  46. _ = m.del(key)
  47. //过期后删除
  48. return nil, nil
  49. }
  50. return item, nil
  51. default:
  52. err = fmt.Errorf("value of %s type error", key)
  53. return nil, err
  54. }
  55. }
  56. func (m *Memory) Set(key string, val interface{}, expire int) error {
  57. s, err := cast.ToStringE(val)
  58. if err != nil {
  59. return err
  60. }
  61. item := &item{
  62. Value: s,
  63. Expired: time.Now().Add(time.Duration(expire) * time.Second),
  64. }
  65. return m.setItem(key, item)
  66. }
  67. func (m *Memory) setItem(key string, item *item) error {
  68. m.items.Store(key, item)
  69. return nil
  70. }
  71. func (m *Memory) Del(key string) error {
  72. return m.del(key)
  73. }
  74. func (m *Memory) del(key string) error {
  75. m.items.Delete(key)
  76. return nil
  77. }
  78. func (m *Memory) HashGet(hk, key string) (string, error) {
  79. item, err := m.getItem(hk + key)
  80. if err != nil || item == nil {
  81. return "", err
  82. }
  83. return item.Value, err
  84. }
  85. func (m *Memory) HashDel(hk, key string) error {
  86. return m.del(hk + key)
  87. }
  88. func (m *Memory) Increase(key string) error {
  89. return m.calculate(key, 1)
  90. }
  91. func (m *Memory) Decrease(key string) error {
  92. return m.calculate(key, -1)
  93. }
  94. func (m *Memory) calculate(key string, num int) error {
  95. m.mutex.RLock()
  96. defer m.mutex.RUnlock()
  97. item, err := m.getItem(key)
  98. if err != nil {
  99. return err
  100. }
  101. if item == nil {
  102. err = fmt.Errorf("%s not exist", key)
  103. return err
  104. }
  105. var n int
  106. n, err = cast.ToIntE(item.Value)
  107. if err != nil {
  108. return err
  109. }
  110. n += num
  111. item.Value = strconv.Itoa(n)
  112. return m.setItem(key, item)
  113. }
  114. func (m *Memory) Expire(key string, dur time.Duration) error {
  115. m.mutex.RLock()
  116. defer m.mutex.RUnlock()
  117. item, err := m.getItem(key)
  118. if err != nil {
  119. return err
  120. }
  121. if item == nil {
  122. err = fmt.Errorf("%s not exist", key)
  123. return err
  124. }
  125. item.Expired = time.Now().Add(dur)
  126. return m.setItem(key, item)
  127. }