fix read locks
This commit is contained in:
parent
883f310881
commit
2d54824f47
10
main.go
10
main.go
|
@ -74,16 +74,16 @@ func (c *MetadataCache) Delete(o int64) {
|
|||
}
|
||||
|
||||
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
|
||||
c.lock.RLock()
|
||||
defer c.lock.Unlock()
|
||||
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.Unlock()
|
||||
c.lock.RUnlock()
|
||||
tmpobject.loading.Do(func() {
|
||||
// Only do this once even if concurrent requests come in
|
||||
tmpobject.entries = loadDbEntries(c.objectType, o)
|
||||
|
@ -92,10 +92,10 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
|
|||
|
||||
}
|
||||
object.lock.RLock()
|
||||
defer object.lock.Unlock()
|
||||
defer object.lock.RUnlock()
|
||||
// Load entries from db
|
||||
entries, ok := object.entries[k]
|
||||
object.lock.Unlock()
|
||||
object.lock.RUnlock()
|
||||
object.lock.Lock()
|
||||
object.lastHit=time.Now()
|
||||
object.lock.Unlock()
|
||||
|
|
Loading…
Reference in New Issue