keymap.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package timercache
  2. import (
  3. "leafstalk/otherutils/snowflake"
  4. "log"
  5. "sync"
  6. "time"
  7. )
  8. type CacheKeys struct {
  9. locker sync.Mutex
  10. tc *timerCache
  11. queryMap map[int64]*TimerEntry
  12. interval time.Duration
  13. msgSequence *snowflake.Node
  14. }
  15. func NewCacheKeys(d time.Duration) *CacheKeys {
  16. cache := newCache()
  17. cw := &CacheKeys{
  18. tc: cache,
  19. queryMap: make(map[int64]*TimerEntry),
  20. interval: d,
  21. }
  22. cache.SetPrecallBack(cw.preCallBack)
  23. var err error
  24. msgSequence, err := snowflake.NewSecondNode(0, 0)
  25. if err != nil {
  26. log.Panic(err)
  27. }
  28. cw.msgSequence = msgSequence
  29. //runtime.SetFinalizer(cw, Stop)
  30. return cw
  31. }
  32. func StopCacheKeys(c *CacheKeys) {
  33. c.tc.stop <- struct{}{}
  34. }
  35. func (h *CacheKeys) preCallBack(e *TimerEntry) bool {
  36. k, ok := e.GetKey().(int64)
  37. if ok {
  38. h.locker.Lock()
  39. defer h.locker.Unlock()
  40. if _, ok := h.queryMap[k]; ok {
  41. delete(h.queryMap, k)
  42. return true
  43. }
  44. } else {
  45. log.Panicf("type convert fail %v", e.GetKey())
  46. }
  47. return false
  48. }
  49. func (h *CacheKeys) generateMsgId() int64 {
  50. return h.msgSequence.Generate().Int64()
  51. }
  52. func (h *CacheKeys) Add(val interface{}, callBack func(e *TimerEntry), msgId int64) int64 {
  53. h.locker.Lock()
  54. defer h.locker.Unlock()
  55. if msgId == 0 {
  56. msgId = h.generateMsgId()
  57. }
  58. // k := h.generateMsgId()
  59. te := h.tc.Add(h.interval, msgId, val, callBack)
  60. h.queryMap[msgId] = te
  61. return msgId
  62. }
  63. func (h *CacheKeys) Remove(key int64) interface{} {
  64. h.locker.Lock()
  65. defer h.locker.Unlock()
  66. if v, ok := h.queryMap[key]; ok {
  67. val := v.GetVal()
  68. delete(h.queryMap, key)
  69. h.tc.Remove(v)
  70. return val
  71. }
  72. return nil
  73. }
  74. // func (h *CacheKeys) RemoveKey(key int64) {
  75. // delete(h.queryMap, key)
  76. // }
  77. type CustomCacheKeys struct {
  78. locker sync.Mutex
  79. tc *timerCache
  80. queryMap map[string]*TimerEntry
  81. Interval time.Duration
  82. }
  83. func NewCustomKeys(d time.Duration) *CustomCacheKeys {
  84. cache := newCache()
  85. cw := &CustomCacheKeys{
  86. tc: cache,
  87. queryMap: make(map[string]*TimerEntry),
  88. Interval: d,
  89. }
  90. cache.SetPrecallBack(cw.preCallBack)
  91. //runtime.SetFinalizer(cw, Stop)
  92. return cw
  93. }
  94. func StopCustomCacheKeys(c *CustomCacheKeys) {
  95. c.tc.stop <- struct{}{}
  96. }
  97. func (h *CustomCacheKeys) preCallBack(e *TimerEntry) bool {
  98. k, ok := e.GetKey().(string)
  99. if ok {
  100. h.locker.Lock()
  101. defer h.locker.Unlock()
  102. if _, ok := h.queryMap[k]; ok {
  103. delete(h.queryMap, k)
  104. return true
  105. }
  106. } else {
  107. log.Panicf("type convert fail %v", e.GetKey())
  108. }
  109. return false
  110. }
  111. // Add时,如果不在queryMap中,必定不在tc中,可能正在预回调后,或完全不牵涉
  112. // 如果在queryMap中,v处在等待到预回调前的这个阶段中
  113. func (h *CustomCacheKeys) Add(key string, val interface{}, callBack func(e *TimerEntry)) {
  114. h.locker.Lock()
  115. defer h.locker.Unlock()
  116. if v, ok := h.queryMap[key]; ok {
  117. v.SetVal(val, callBack)
  118. return
  119. }
  120. te := h.tc.Add(h.Interval, key, val, callBack)
  121. h.queryMap[key] = te
  122. }
  123. // queryMap中存在时,v处在等待到预执行前的这个阶段中
  124. func (h *CustomCacheKeys) Remove(key string) {
  125. h.locker.Lock()
  126. defer h.locker.Unlock()
  127. if v, ok := h.queryMap[key]; ok {
  128. delete(h.queryMap, key)
  129. h.tc.Remove(v)
  130. }
  131. }
  132. // func (h *CustomCacheKeys) RemoveKey(key string) {
  133. // delete(h.queryMap, key)
  134. // }