cache.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "context"
  17. "sync"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. )
  21. type item struct {
  22. data []api.SourceTuple
  23. expiration int64
  24. }
  25. type Cache struct {
  26. expireTime int
  27. cacheMissingKey bool
  28. cancel context.CancelFunc
  29. items map[string]*item
  30. sync.RWMutex
  31. }
  32. func NewCache(expireTime int, cacheMissingKey bool) *Cache {
  33. c := &Cache{
  34. expireTime: expireTime,
  35. cacheMissingKey: cacheMissingKey,
  36. items: make(map[string]*item),
  37. }
  38. if expireTime > 0 {
  39. ctx, cancel := context.WithCancel(context.Background())
  40. c.cancel = cancel
  41. go c.run(ctx)
  42. }
  43. return c
  44. }
  45. func (c *Cache) run(ctx context.Context) {
  46. ticker := conf.GetTicker(c.expireTime * 2000)
  47. for {
  48. select {
  49. case <-ticker.C:
  50. c.deleteExpired()
  51. case <-ctx.Done():
  52. conf.Log.Infof("Lookup cache is stopped")
  53. ticker.Stop()
  54. return
  55. }
  56. }
  57. }
  58. func (c *Cache) deleteExpired() {
  59. now := conf.GetNowInMilli()
  60. c.Lock()
  61. for k, v := range c.items {
  62. if v.expiration > 0 && now > v.expiration {
  63. delete(c.items, k)
  64. }
  65. }
  66. c.Unlock()
  67. }
  68. func (c *Cache) Set(key string, value []api.SourceTuple) {
  69. if (value == nil || len(value) == 0) && !c.cacheMissingKey {
  70. return
  71. }
  72. c.Lock()
  73. defer c.Unlock()
  74. if c.expireTime > 0 {
  75. c.items[key] = &item{data: value, expiration: conf.GetNowInMilli() + int64(c.expireTime*1000)}
  76. } else {
  77. c.items[key] = &item{data: value}
  78. }
  79. }
  80. func (c *Cache) Get(key string) ([]api.SourceTuple, bool) {
  81. c.RLock()
  82. defer c.RUnlock()
  83. if v, ok := c.items[key]; ok {
  84. if v.expiration > 0 && conf.GetNowInMilli() > v.expiration {
  85. return nil, false
  86. }
  87. return v.data, true
  88. }
  89. return nil, false
  90. }
  91. func (c *Cache) Close() {
  92. if c.cancel != nil {
  93. c.cancel()
  94. }
  95. c.items = nil
  96. }