implemented get keys for prefix

This commit is contained in:
Radu Ioan Fericean
2016-08-16 18:11:38 +03:00
parent f37a21319c
commit a7f3ae99c4
5 changed files with 15 additions and 58 deletions

View File

@@ -132,12 +132,6 @@ func CountEntries(prefix string) (result int) {
return cache.CountEntriesForPrefix(prefix)
}
func GetAllEntries(prefix string) map[string]interface{} {
mux.RLock()
defer mux.RUnlock()
return cache.GetAllForPrefix(prefix)
}
func GetEntriesKeys(prefix string) (keys []string) {
mux.RLock()
defer mux.RUnlock()

View File

@@ -14,7 +14,6 @@ type cacheStore interface {
Delete(string)
DeletePrefix(string)
CountEntriesForPrefix(string) int
GetAllForPrefix(string) map[string]interface{}
GetKeysForPrefix(string) []string
}
@@ -33,9 +32,6 @@ func (cs cacheDoubleStore) Put(key string, value interface{}) {
cs[prefix] = mp
}
mp[key] = value
/*if err := dumper.put(prefix, key, value); err != nil {
utils.Logger.Info("<cache dumper> put error: " + err.Error())
}*/
}
func (cs cacheDoubleStore) Get(key string) (interface{}, bool) {
@@ -52,18 +48,11 @@ func (cs cacheDoubleStore) Delete(key string) {
prefix, key := key[:PREFIX_LEN], key[PREFIX_LEN:]
if keyMap, ok := cs[prefix]; ok {
delete(keyMap, key)
/*if err := dumper.delete(prefix, key); err != nil {
utils.Logger.Info("<cache dumper> delete error: " + err.Error())
}*/
}
}
func (cs cacheDoubleStore) DeletePrefix(prefix string) {
delete(cs, prefix)
/*if err := dumper.deleteAll(prefix); err != nil {
utils.Logger.Info("<cache dumper> delete all error: " + err.Error())
}*/
}
func (cs cacheDoubleStore) CountEntriesForPrefix(prefix string) int {
@@ -73,13 +62,6 @@ func (cs cacheDoubleStore) CountEntriesForPrefix(prefix string) int {
return 0
}
func (cs cacheDoubleStore) GetAllForPrefix(prefix string) map[string]interface{} {
if keyMap, ok := cs[prefix]; ok {
return keyMap
}
return nil
}
func (cs cacheDoubleStore) GetKeysForPrefix(prefix string) (keys []string) {
prefix, key := prefix[:PREFIX_LEN], prefix[PREFIX_LEN:]
if keyMap, ok := cs[prefix]; ok {
@@ -198,18 +180,18 @@ func (cs cacheLRUTTL) CountEntriesForPrefix(prefix string) int {
return 0
}
func (cs cacheLRUTTL) GetAllForPrefix(prefix string) map[string]interface{} {
return nil
}
func (cs cacheLRUTTL) GetKeysForPrefix(prefix string) (keys []string) {
prefix, key := prefix[:PREFIX_LEN], prefix[PREFIX_LEN:]
if keyMap, ok := cs[prefix]; ok {
for iterKey := range keyMap.cache {
if len(key) == 0 || strings.HasPrefix(iterKey, key) {
keys = append(keys, prefix+iterKey)
}
}
}
return nil
}
func (cs cacheLRUTTL) Load(path string, prefixes []string) error {
return utils.ErrNotImplemented
}
// faster to access
type cacheSimpleStore struct {
cache map[string]interface{}
@@ -286,21 +268,6 @@ func (cs cacheSimpleStore) CountEntriesForPrefix(prefix string) int {
return 0
}
func (cs cacheSimpleStore) GetAllForPrefix(prefix string) map[string]interface{} {
result := make(map[string]interface{})
found := false
for key, ti := range cs.cache {
if strings.HasPrefix(key, prefix) {
result[key[PREFIX_LEN:]] = ti
found = true
}
}
if !found {
return nil
}
return result
}
func (cs cacheSimpleStore) GetKeysForPrefix(prefix string) (keys []string) {
for key, _ := range cs.cache {
if strings.HasPrefix(key, prefix) {
@@ -309,7 +276,3 @@ func (cs cacheSimpleStore) GetKeysForPrefix(prefix string) (keys []string) {
}
return
}
func (cs cacheSimpleStore) Load(path string, keys []string) error {
return utils.ErrNotImplemented
}

View File

@@ -19,7 +19,7 @@ type Cache struct {
lruIndex *list.List
ttlIndex []*list.Element
cache map[interface{}]*list.Element
cache map[string]*list.Element
expiration time.Duration
}
@@ -38,7 +38,7 @@ func NewLRUTTL(maxEntries int, expire time.Duration) *Cache {
expiration: expire,
lruIndex: list.New(),
ttlIndex: make([]*list.Element, 0),
cache: make(map[interface{}]*list.Element),
cache: make(map[string]*list.Element),
}
if c.expiration > 0 {
go c.cleanExpired()
@@ -49,11 +49,12 @@ func NewLRUTTL(maxEntries int, expire time.Duration) *Cache {
// cleans expired entries performing minimal checks
func (c *Cache) cleanExpired() {
for {
c.mu.RLock()
if len(c.ttlIndex) == 0 {
c.mu.RUnlock()
time.Sleep(c.expiration)
continue
}
c.mu.RLock()
e := c.ttlIndex[0]
c.mu.RUnlock()
@@ -72,7 +73,7 @@ func (c *Cache) cleanExpired() {
func (c *Cache) Set(key string, value interface{}) {
c.mu.Lock()
if c.cache == nil {
c.cache = make(map[interface{}]*list.Element)
c.cache = make(map[string]*list.Element)
c.lruIndex = list.New()
c.ttlIndex = make([]*list.Element, 0)
}
@@ -173,5 +174,5 @@ func (c *Cache) Flush() {
defer c.mu.Unlock()
c.lruIndex = list.New()
c.ttlIndex = make([]*list.Element, 0)
c.cache = make(map[interface{}]*list.Element)
c.cache = make(map[string]*list.Element)
}

View File

@@ -62,7 +62,7 @@ func TestLRUandExpire(t *testing.T) {
if last != 8 {
t.Error("error dicarding least recently used entry: ", last)
}
time.Sleep(5 * time.Millisecond)
time.Sleep(6 * time.Millisecond)
if cache.Len() != 0 {
t.Error("error dicarding expired entries: ", cache.Len())
}

View File

@@ -191,7 +191,6 @@ func TestStorageCacheGetReverseAliases(t *testing.T) {
t.Error("Error getting reverse alias: ", aliasKeys, ala.GetId()+utils.ANY)
}
} else {
t.Log(utils.ToIJSON(cache2go.GetAllEntries(utils.REVERSE_ALIASES_PREFIX)))
t.Error("Error getting reverse alias: ", err)
}
accountingStorage.GetReverseAlias("aaa"+"Account"+"*other", false)