Package cache contains caching functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gc.go 482B

12345678910111213141516171819202122232425262728293031323334
  1. package cache
  2. import "time"
  3. type GC struct {
  4. Interval time.Duration
  5. Cache Cache
  6. stop chan struct{}
  7. }
  8. func NewGC(interval time.Duration, cache Cache) *GC {
  9. return &GC{
  10. Interval: interval,
  11. Cache: cache,
  12. stop: make(chan struct{}),
  13. }
  14. }
  15. func (gc *GC) Run() {
  16. ticker := time.NewTicker(gc.Interval)
  17. for {
  18. select {
  19. case <-ticker.C:
  20. gc.Cache.RunGC()
  21. case <-gc.stop:
  22. ticker.Stop()
  23. return
  24. }
  25. }
  26. }
  27. func (gc *GC) Stop() {
  28. gc.stop <- struct{}{}
  29. }