GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cache-obj/sweep.go at main · ubgo/cache-obj

Expand file tree

/

sweep.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
61 lines (55 loc) · 1.68 KB
/

sweep.go

Copy path

File metadata and controls

61 lines (55 loc) · 1.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// sweep.go — the optional background expiry sweeper + Close lifecycle
// (package cacheobj, github.com/ubgo/cache-obj).
//
// Without a sweeper, expiry is lazy: an entry past its TTL is only reclaimed
// on the Get that touches it, so an expired key that is never read again
// lingers until LRU capacity evicts it. WithSweepInterval starts a goroutine
// that periodically scans and evicts expired entries, bounding that memory.
// The cost is a goroutine that must be stopped with Close.
package cacheobj
import "time"
// sweepLoop runs until Close: on each tick it evicts expired entries.
func (s *Store[T]) sweepLoop(interval time.Duration) {
t := time.NewTicker(interval)
defer t.Stop()
for {
select {
case <-t.C:
s.sweep()
case <-s.done:
return
}
}
}
// sweep removes every expired entry in one pass, firing OnEvict
// (cache.EvictExpired) for each. It holds the lock for the whole scan, so the
// interval should suit the cache size; keep OnEvict fast. Uses Peek so the
// scan does not disturb LRU recency.
func (s *Store[T]) sweep() {
s.mu.Lock()
defer s.mu.Unlock()
if s.lru != nil {
for _, key := range s.lru.Keys() {
if e, ok := s.lru.Peek(key); ok && s.expired(e) {
s.evictExpired(key, e.value)
}
}
return
}
for key, e := range s.m {
if s.expired(e) {
s.evictExpired(key, e.value)
}
}
}
// Close stops the background sweeper started by WithSweepInterval. It is
// idempotent and safe to call even when no sweeper was started (a no-op). The
// cache remains usable after Close — only the background goroutine stops;
// expiry reverts to lazy (on Get).
func (s *Store[T]) Close() {
s.closeOnce.Do(func() {
if s.done != nil {
close(s.done)
}
})
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.