Skip to content

Commit d229745

Browse files
committed
added Shutdown
1 parent 77adc1d commit d229745

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

ramcache.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Ramcache struct {
2929
TTL time.Duration
3030
MaxAge time.Duration
3131
frozen bool
32+
done chan bool
3233
sync.RWMutex
3334
}
3435

@@ -38,7 +39,8 @@ type Ramcache struct {
3839
func New() *Ramcache {
3940
tq := make(timeQueue, 0)
4041
c := make(map[string]*item)
41-
r := &Ramcache{cache: c, tqueue: tq, TTL: 5 * time.Minute}
42+
d := make(chan bool)
43+
r := &Ramcache{cache: c, tqueue: tq, TTL: 5 * time.Minute, done: d}
4244
go r.cleanup()
4345
return r
4446
}
@@ -151,10 +153,20 @@ func (rc *Ramcache) Keys() []string {
151153
return result
152154
}
153155

156+
// Shutdown cleanly stops any background work, allowing Ramcache
157+
// to be garbage collected.
158+
func (rc *Ramcache) Shutdown() {
159+
close(rc.done)
160+
}
161+
154162
func (rc *Ramcache) cleanup() {
155163
for {
156-
time.Sleep(10 * time.Second)
157-
rc.clean(time.Now())
164+
select {
165+
case <-time.After(10 * time.Second):
166+
rc.clean(time.Now())
167+
case <-rc.done:
168+
return
169+
}
158170
}
159171
}
160172

0 commit comments

Comments
 (0)