Merge pull request #624 from vadmeste/pr_out_protect_exported_memory_object_api_by_a_mutex

Protect the exported method Delete() by a lock
This commit is contained in:
Harshavardhana 2015-05-31 12:34:02 -07:00
commit 122d67625f
2 changed files with 9 additions and 3 deletions

View File

@ -128,7 +128,7 @@ func (r *Cache) Set(key string, value interface{}) bool {
// remove random key if only we reach the maxSize threshold // remove random key if only we reach the maxSize threshold
for key := range r.items { for key := range r.items {
for (r.currentSize + valueLen) > r.maxSize { for (r.currentSize + valueLen) > r.maxSize {
r.Delete(key) r.doDelete(key)
} }
break break
} }
@ -145,13 +145,19 @@ func (r *Cache) Expire() {
defer r.Unlock() defer r.Unlock()
for key := range r.items { for key := range r.items {
if !r.isValid(key) { if !r.isValid(key) {
r.Delete(key) r.doDelete(key)
} }
} }
} }
// Delete deletes a given key if exists // Delete deletes a given key if exists
func (r *Cache) Delete(key string) { func (r *Cache) Delete(key string) {
r.Lock()
defer r.Unlock()
r.doDelete(key)
}
func (r *Cache) doDelete(key string) {
if _, ok := r.items[key]; ok { if _, ok := r.items[key]; ok {
r.currentSize -= uint64(len(r.items[key].([]byte))) r.currentSize -= uint64(len(r.items[key].([]byte)))
delete(r.items, key) delete(r.items, key)

View File

@ -212,7 +212,7 @@ func (memory *memoryDriver) cleanupMultiparts(bucket, key, uploadID string) {
defer memory.lock.Unlock() defer memory.lock.Unlock()
for i := 1; i <= memory.storedBuckets[bucket].multiPartSession[key].totalParts; i++ { for i := 1; i <= memory.storedBuckets[bucket].multiPartSession[key].totalParts; i++ {
objectKey := bucket + "/" + getMultipartKey(key, uploadID, i) objectKey := bucket + "/" + getMultipartKey(key, uploadID, i)
memory.multiPartObjects.Delete(objectKey) memory.multiPartObjects.doDelete(objectKey)
} }
} }