From d7ef1bb9493a693a88f39cdd89f88c7aa9e04d1f Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 16 Aug 2021 18:19:42 -0400 Subject: [PATCH] mutexes --- main.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 1aa9136..b6c5dd6 100644 --- a/main.go +++ b/main.go @@ -77,13 +77,11 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) { log.Printf("Get %d, %s.\n", o, k) // Check cache for entry c.lock.RLock() - defer c.lock.RUnlock() object, ok := c.objects[o] if !ok { // Object is not in the cache so let's load it up var tmpobject metadataObject c.objects[o] = tmpobject - c.lock.RUnlock() tmpobject.loading.Do(func() { // Only do this once even if concurrent requests come in tmpobject.entries = loadDbEntries(c.objectType, o) @@ -91,11 +89,12 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) { object = tmpobject } + c.lock.RUnlock() + object.lock.RLock() - defer object.lock.RUnlock() - // Load entries from db entries, ok := object.entries[k] object.lock.RUnlock() + object.lock.Lock() object.lastHit=time.Now() object.lock.Unlock()