fix read locks

This commit is contained in:
Your Name 2021-08-16 18:15:24 -04:00
parent 883f310881
commit 2d54824f47
1 changed files with 5 additions and 5 deletions

10
main.go
View File

@ -74,16 +74,16 @@ func (c *MetadataCache) Delete(o int64) {
} }
func (c *MetadataCache) Get(o int64, k string) ([]string, error) { func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
log.Printf("Get %d, %s", 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.Unlock() 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.Unlock() 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)
@ -92,10 +92,10 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
} }
object.lock.RLock() object.lock.RLock()
defer object.lock.Unlock() defer object.lock.RUnlock()
// Load entries from db // Load entries from db
entries, ok := object.entries[k] entries, ok := object.entries[k]
object.lock.Unlock() object.lock.RUnlock()
object.lock.Lock() object.lock.Lock()
object.lastHit=time.Now() object.lastHit=time.Now()
object.lock.Unlock() object.lock.Unlock()