dcache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package dcache
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. var (
  7. // ErrKeyAlreadyInUse is returned when setting an entry on an exist key.
  8. ErrKeyAlreadyInUse = errors.New("key already in use")
  9. // ErrKeyDoestNotExist is returned when retrieving a non existant key.
  10. ErrKeyDoestNotExist = errors.New("key does not exist")
  11. )
  12. type DCache struct {
  13. tick time.Duration
  14. entries map[string]any
  15. queue []string
  16. out chan *Collect
  17. stopWorker chan bool
  18. }
  19. type Collect struct {
  20. key string
  21. val any
  22. }
  23. func New(tick time.Duration) *DCache {
  24. if tick == 0 {
  25. tick = time.Second
  26. }
  27. return &DCache{
  28. tick: tick,
  29. entries: make(map[string]any),
  30. queue: []string{},
  31. out: make(chan *Collect),
  32. stopWorker: make(chan bool),
  33. }
  34. }
  35. func (c *DCache) push(key string) {
  36. c.queue = append(c.queue, key)
  37. }
  38. func (c *DCache) pop() string {
  39. if len(c.queue) < 1 {
  40. return ""
  41. }
  42. key := c.queue[0]
  43. c.queue = c.queue[1:]
  44. return key
  45. }
  46. func (c *DCache) startWorker() {
  47. tick := time.Tick(c.tick)
  48. for {
  49. select {
  50. case <-tick:
  51. key := c.pop()
  52. entry, err := c.Get(key)
  53. if err != nil {
  54. continue
  55. }
  56. delete(c.entries, key)
  57. c.out <- &Collect{
  58. key: key,
  59. val: entry,
  60. }
  61. case <-c.stopWorker:
  62. return
  63. }
  64. }
  65. }
  66. // StartCycle starts the worker in a go routine.
  67. func (c *DCache) StartCycle() {
  68. go c.startWorker()
  69. }
  70. // StopCycle signals any worker to stop.
  71. func (c *DCache) StopCycle() {
  72. c.stopWorker <- true
  73. }
  74. // Collect delayed entries removed from cache.
  75. func (c *DCache) Collect() <-chan *Collect {
  76. return c.out
  77. }
  78. // Get reads an entry from cache.
  79. func (c *DCache) Get(key string) (any, error) {
  80. entry, ok := c.entries[key]
  81. if !ok {
  82. return nil, ErrKeyDoestNotExist
  83. }
  84. return entry, nil
  85. }
  86. // Set saves an entry to the cache.
  87. func (c *DCache) Set(key string, val any) error {
  88. if has := c.Has(key); has {
  89. return ErrKeyAlreadyInUse
  90. }
  91. c.entries[key] = val
  92. c.push(key)
  93. return nil
  94. }
  95. // Has is wrapper around get to check if an entry still exist in the cache.
  96. func (c *DCache) Has(key string) bool {
  97. _, err := c.Get(key)
  98. if err != ErrKeyDoestNotExist {
  99. return true
  100. }
  101. return false
  102. }
  103. // Remove deletes an entry from the cache and:
  104. //
  105. // silent false: send the val to the out channel to collect.
  106. // silent true: val cannot be collected.
  107. func (c *DCache) Remove(key string, silent bool) error {
  108. entry, err := c.Get(key)
  109. if err != nil {
  110. return err
  111. }
  112. if !silent {
  113. c.out <- &Collect{
  114. key: key,
  115. val: entry,
  116. }
  117. }
  118. delete(c.entries, key)
  119. return nil
  120. }
  121. // Size returns the number of entries currently stored.
  122. func (c *DCache) Size() int {
  123. return len(c.entries)
  124. }