Simplified locking for lrustore

This commit is contained in:
DanB
2016-08-30 13:13:26 +02:00
parent ffbdf717ff
commit 17651acf8c
3 changed files with 9 additions and 8 deletions

View File

@@ -2,7 +2,6 @@
package cache2go
import (
"fmt"
"strings"
"sync"
@@ -95,7 +94,7 @@ func (cs cacheDoubleStore) GetKeysForPrefix(prefix string) (keys []string) {
// easy to be counted exported by prefix
type lrustore struct {
store map[string]*lru.Cache
sync.RWMutex
sync.Mutex
}
func newLruStore() *lrustore {
@@ -184,8 +183,8 @@ func (cs lrustore) Put(key string, value interface{}) {
}
func (cs lrustore) Get(key string) (interface{}, bool) {
cs.RLock()
defer cs.RUnlock()
cs.Lock()
defer cs.Unlock()
prefix, key := key[:PREFIX_LEN], key[PREFIX_LEN:]
if keyMap, ok := cs.store[prefix]; ok {
if ti, exists := keyMap.Get(key); exists {
@@ -211,8 +210,8 @@ func (cs lrustore) DeletePrefix(prefix string) {
}
func (cs lrustore) CountEntriesForPrefix(prefix string) int {
cs.RLock()
defer cs.RUnlock()
cs.Lock()
defer cs.Unlock()
if m, ok := cs.store[prefix]; ok {
return m.Len()
}

View File

@@ -1,6 +1,8 @@
package cache2go
import "testing"
import (
"testing"
)
func TestRemKey(t *testing.T) {
Set("t11_mm", "test")

View File

@@ -1,4 +1,4 @@
#! /usr/bin/env sh
./build.sh
go test $(glide novendor)
exit $?
exit $?