This commit is contained in:
Your Name 2021-08-16 18:19:42 -04:00
parent 2d54824f47
commit d7ef1bb949
1 changed files with 3 additions and 4 deletions

View File

@ -77,13 +77,11 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
log.Printf("Get %d, %s.\n", o, k) log.Printf("Get %d, %s.\n", o, k)
// Check cache for entry // Check cache for entry
c.lock.RLock() c.lock.RLock()
defer c.lock.RUnlock()
object, ok := c.objects[o] object, ok := c.objects[o]
if !ok { if !ok {
// Object is not in the cache so let's load it up // Object is not in the cache so let's load it up
var tmpobject metadataObject var tmpobject metadataObject
c.objects[o] = tmpobject c.objects[o] = tmpobject
c.lock.RUnlock()
tmpobject.loading.Do(func() { tmpobject.loading.Do(func() {
// Only do this once even if concurrent requests come in // Only do this once even if concurrent requests come in
tmpobject.entries = loadDbEntries(c.objectType, o) tmpobject.entries = loadDbEntries(c.objectType, o)
@ -91,11 +89,12 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
object = tmpobject object = tmpobject
} }
c.lock.RUnlock()
object.lock.RLock() object.lock.RLock()
defer object.lock.RUnlock()
// Load entries from db
entries, ok := object.entries[k] entries, ok := object.entries[k]
object.lock.RUnlock() object.lock.RUnlock()
object.lock.Lock() object.lock.Lock()
object.lastHit=time.Now() object.lastHit=time.Now()
object.lock.Unlock() object.lock.Unlock()