File tree Expand file tree Collapse file tree 1 file changed +15
-3
lines changed
Expand file tree Collapse file tree 1 file changed +15
-3
lines changed Original file line number Diff line number Diff 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 {
3839func 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+
154162func (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
You can’t perform that action at this time.
0 commit comments