diff --git a/cache.go b/cache.go index c9f3cac3..420d1a69 100644 --- a/cache.go +++ b/cache.go @@ -131,6 +131,7 @@ type Item struct { Value interface{} Cost int64 Expiration time.Time + wg *sync.WaitGroup } // NewCache returns a new Cache instance and any configuration errors, if any. @@ -169,6 +170,16 @@ func NewCache(config *Config) (*Cache, error) { return cache, nil } +func (c *Cache) Wait() { + if c == nil { + return + } + wg := &sync.WaitGroup{} + wg.Add(1) + c.setBuf <- &Item{wg: wg} + wg.Wait() +} + // Get returns the value (if any) and a boolean representing whether the // value was found or not. The value can be nil and the boolean can be true at // the same time. @@ -346,6 +357,10 @@ func (c *Cache) processItems() { for { select { case i := <-c.setBuf: + if i.wg != nil { + i.wg.Done() + continue + } // Calculate item cost value if new or update. if i.Cost == 0 && c.cost != nil && i.flag != itemDelete { i.Cost = c.cost(i.Value)