fix(azure::cache): TimedCache.Getter should be called once on the same key

If a key have't been saved in TimedCache, and there are multiple
goroutines getting this key, TimedCache.Getter might be invoked multiple
times because the entry is overwritten in TimedCache.getInternal.

Signed-off-by: knight42 <anonymousknight96@gmail.com>
This commit is contained in:
knight42 2020-09-05 02:43:53 +08:00
parent 90ddd5f721
commit 2b1395a88a
No known key found for this signature in database
GPG Key ID: 1040B69865E7D86C

View File

@ -103,6 +103,15 @@ func (t *TimedCache) getInternal(key string) (*AzureCacheEntry, error) {
t.Lock.Lock()
defer t.Lock.Unlock()
// Another goroutine might have written the same key.
entry, exists, err = t.Store.GetByKey(key)
if err != nil {
return nil, err
}
if exists {
return entry.(*AzureCacheEntry), nil
}
// Still not found, add new entry with nil data.
// Note the data will be filled later by getter.
newEntry := &AzureCacheEntry{