| 1 | package gocachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ |
| 2 | |
| 3 | import ( |
| 4 | "logscip-go gomod github.com/golang/go/src go1.24.4 log/" |
| 5 | "timescip-go gomod github.com/golang/go/src go1.24.4 time/" |
| 6 | ) |
| 7 | |
| 8 | const ( |
| 9 | // JanitorShiftTarget is the target number of expired keys to find during passive clean up duty |
| 10 | // before pausing the passive expired keys eviction process |
| 11 | JanitorShiftTargetscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorShiftTarget. = 25 |
| 12 | |
| 13 | // JanitorMaxIterationsPerShift is the maximum number of nodes to traverse before pausing |
| 14 | // |
| 15 | // This is to prevent the janitor from traversing the entire cache, which could take a long time |
| 16 | // to complete depending on the size of the cache. |
| 17 | // |
| 18 | // By limiting it to a small number, we are effectively reducing the impact of passive eviction. |
| 19 | JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift. = 1000 |
| 20 | |
| 21 | // JanitorMinShiftBackOff is the minimum interval between each iteration of steps |
| 22 | // defined by JanitorMaxIterationsPerShift |
| 23 | JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. = 50 * timescip-go gomod github.com/golang/go/src go1.24.4 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.4 time/Millisecond. |
| 24 | |
| 25 | // JanitorMaxShiftBackOff is the maximum interval between each iteration of steps |
| 26 | // defined by JanitorMaxIterationsPerShift |
| 27 | JanitorMaxShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxShiftBackOff. = 500 * timescip-go gomod github.com/golang/go/src go1.24.4 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.4 time/Millisecond. |
| 28 | ) |
| 29 | |
| 30 | // StartJanitor starts the janitor on a different goroutine |
| 31 | // The janitor's job is to delete expired keys in the background, in other words, it takes care of passive eviction. |
| 32 | // It can be stopped by calling Cache.StopJanitor. |
| 33 | // If you do not start the janitor, expired keys will only be deleted when they are accessed through Get, GetByKeys, or |
| 34 | // GetAll. |
| 35 | func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().() error { |
| 36 | if cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. != nil { |
| 37 | return ErrJanitorAlreadyRunningscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ErrJanitorAlreadyRunning. |
| 38 | } |
| 39 | cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. = make(chan bool) |
| 40 | go func() { |
| 41 | // rather than starting from the tail on every run, we can try to start from the last traversed entry |
| 42 | var lastTraversedNode *Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry# |
| 43 | totalNumberOfExpiredKeysInPreviousRunFromTailToHead := 0 |
| 44 | backOff := JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. |
| 45 | for { |
| 46 | select { |
| 47 | case <-timescip-go gomod github.com/golang/go/src go1.24.4 time/.Afterscip-go gomod github.com/golang/go/src go1.24.4 time/After().(backOff): |
| 48 | // Passive clean up duty |
| 49 | cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().() |
| 50 | if cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. != nil { |
| 51 | start := timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nowscip-go gomod github.com/golang/go/src go1.24.4 time/Now().() |
| 52 | steps := 0 |
| 53 | expiredEntriesFound := 0 |
| 54 | current := cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. |
| 55 | if lastTraversedNode != nil { |
| 56 | // Make sure the lastTraversedNode is still in the cache, otherwise we might be traversing nodes that were already deleted. |
| 57 | // Furthermore, we need to make sure that the entry from the cache has the same pointer as the lastTraversedNode |
| 58 | // to verify that there isn't just a new cache entry with the same key (i.e. in case lastTraversedNode got evicted) |
| 59 | if entryFromCache, isInCache := cache.getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#get().(lastTraversedNode.Keyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Key.); isInCache && entryFromCache == lastTraversedNode { |
| 60 | current = lastTraversedNode |
| 61 | } |
| 62 | } |
| 63 | if current == cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. { |
| 64 | if Debugscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Debug. { |
| 65 | logscip-go gomod github.com/golang/go/src go1.24.4 log/.Printfscip-go gomod github.com/golang/go/src go1.24.4 log/Printf().("There are currently %d entries in the cache. The last walk resulted in finding %d expired keys", len(cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.), totalNumberOfExpiredKeysInPreviousRunFromTailToHead) |
| 66 | } |
| 67 | totalNumberOfExpiredKeysInPreviousRunFromTailToHead = 0 |
| 68 | } |
| 69 | for current != nil { |
| 70 | // since we're walking from the tail to the head, we get the previous reference |
| 71 | var previous *Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry# |
| 72 | steps++ |
| 73 | if current.Expiredscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expired().() { |
| 74 | expiredEntriesFound++ |
| 75 | // Because delete will remove the previous reference from the entry, we need to store the |
| 76 | // previous reference before we delete it |
| 77 | previous = current.previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. |
| 78 | cache.deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#delete().(current.Keyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Key.) |
| 79 | cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..ExpiredKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#ExpiredKeys.++ |
| 80 | } |
| 81 | if current == cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. { |
| 82 | lastTraversedNode = nil |
| 83 | break |
| 84 | } |
| 85 | // Travel to the current node's previous node only if no specific previous node has been specified |
| 86 | if previous != nil { |
| 87 | current = previous |
| 88 | } else { |
| 89 | current = current.previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. |
| 90 | } |
| 91 | lastTraversedNode = current |
| 92 | if steps == JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift. || expiredEntriesFound >= JanitorShiftTargetscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorShiftTarget. { |
| 93 | if expiredEntriesFound > 0 { |
| 94 | backOff = JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. |
| 95 | } else { |
| 96 | if backOff*2 <= JanitorMaxShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxShiftBackOff. { |
| 97 | backOff *= 2 |
| 98 | } else { |
| 99 | backOff = JanitorMaxShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxShiftBackOff. |
| 100 | } |
| 101 | } |
| 102 | break |
| 103 | } |
| 104 | } |
| 105 | if Debugscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Debug. { |
| 106 | logscip-go gomod github.com/golang/go/src go1.24.4 log/.Printfscip-go gomod github.com/golang/go/src go1.24.4 log/Printf().("traversed %d nodes and found %d expired entries in %s before stopping\n", steps, expiredEntriesFound, timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sincescip-go gomod github.com/golang/go/src go1.24.4 time/Since().(start)) |
| 107 | } |
| 108 | totalNumberOfExpiredKeysInPreviousRunFromTailToHead += expiredEntriesFound |
| 109 | } else { |
| 110 | if backOff*2 < JanitorMaxShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxShiftBackOff. { |
| 111 | backOff *= 2 |
| 112 | } else { |
| 113 | backOff = JanitorMaxShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxShiftBackOff. |
| 114 | } |
| 115 | } |
| 116 | cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().() |
| 117 | case <-cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor.: |
| 118 | cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. <- true |
| 119 | return |
| 120 | } |
| 121 | } |
| 122 | }() |
| 123 | //if Debug { |
| 124 | // go func() { |
| 125 | // var m runtime.MemStats |
| 126 | // for { |
| 127 | // runtime.ReadMemStats(&m) |
| 128 | // log.Printf("Alloc=%vMB; HeapReleased=%vMB; Sys=%vMB; HeapInUse=%vMB; HeapObjects=%v; HeapObjectsFreed=%v; GC=%v; cache.memoryUsage=%vMB; cacheSize=%d\n", m.Alloc/1024/1024, m.HeapReleased/1024/1024, m.Sys/1024/1024, m.HeapInuse/1024/1024, m.HeapObjects, m.Frees, m.NumGC, cache.memoryUsage/1024/1024, cache.Count()) |
| 129 | // time.Sleep(3 * time.Second) |
| 130 | // } |
| 131 | // }() |
| 132 | //} |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | // StopJanitor stops the janitor |
| 137 | func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() { |
| 138 | if cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. != nil { |
| 139 | // Tell the janitor to stop, and then wait for the janitor to reply on the same channel that it's stopping |
| 140 | // This may seem a bit odd, but this allows us to avoid a data race condition when trying to set |
| 141 | // cache.stopJanitor to nil |
| 142 | cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. <- true |
| 143 | <-cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. |
| 144 | cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. = nil |
| 145 | } |
| 146 | } |