diff --git a/layeredcache.go b/layeredcache.go index 747661a..e064eed 100644 --- a/layeredcache.go +++ b/layeredcache.go @@ -70,10 +70,12 @@ func (c *LayeredCache) Get(primary, secondary string) *Item { func (c *LayeredCache) GetOrCreateSecondaryCache(primary string) *SecondaryCache { primaryBkt := c.bucket(primary) bkt := primaryBkt.getSecondaryBucket(primary) + primaryBkt.Lock() if bkt == nil { bkt = &bucket{lookup: make(map[string]*Item)} primaryBkt.buckets[primary] = bkt } + primaryBkt.Unlock() return &SecondaryCache{ bucket: bkt, pCache: c, diff --git a/secondarycache.go b/secondarycache.go index 03a34c0..f901fde 100644 --- a/secondarycache.go +++ b/secondarycache.go @@ -26,7 +26,7 @@ func (s *SecondaryCache) Set(secondary string, value interface{}, duration time. // Fetch or set a secondary key. // The semantics are the same as for LayeredCache.Fetch -func (s *SecondaryCache) Fetch(secondary string, duration time.Duration, fetch func() (interface{}, error)) (interface{}, error) { +func (s *SecondaryCache) Fetch(secondary string, duration time.Duration, fetch func() (interface{}, error)) (*Item, error) { item := s.Get(secondary) if item != nil { return item, nil diff --git a/secondarycache_test.go b/secondarycache_test.go index 2ac2453..8b733c1 100644 --- a/secondarycache_test.go +++ b/secondarycache_test.go @@ -78,14 +78,14 @@ func (_ SecondaryCacheTests) FetchReturnsAnExistingValue() { cache.Set("spice", "flow", "value-a", time.Minute) sCache := cache.GetOrCreateSecondaryCache("spice") val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) {return "a fetched value", nil}) - Expect(val.(*Item).Value().(string)).To.Equal("value-a") + Expect(val.Value().(string)).To.Equal("value-a") } func (_ SecondaryCacheTests) FetchReturnsANewValue() { cache := newLayered() sCache := cache.GetOrCreateSecondaryCache("spice") val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) {return "a fetched value", nil}) - Expect(val.(*Item).Value().(string)).To.Equal("a fetched value") + Expect(val.Value().(string)).To.Equal("a fetched value") } func (_ SecondaryCacheTests) TrackerDoesNotCleanupHeldInstance() {