From a7f3ae99c4c51b7f84eda393e26bde4d0e837486 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 16 Aug 2016 18:11:38 +0300 Subject: [PATCH] implemented get keys for prefix --- cache2go/cache.go | 6 ----- cache2go/cache_store.go | 53 +++++++---------------------------------- cache2go/lruttl.go | 11 +++++---- cache2go/lruttl_test.go | 2 +- engine/storage_test.go | 1 - 5 files changed, 15 insertions(+), 58 deletions(-) diff --git a/cache2go/cache.go b/cache2go/cache.go index fd0d332e1..bba476f76 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -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() diff --git a/cache2go/cache_store.go b/cache2go/cache_store.go index d9f187566..56dd27bab 100644 --- a/cache2go/cache_store.go +++ b/cache2go/cache_store.go @@ -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(" 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(" delete error: " + err.Error()) - }*/ } } func (cs cacheDoubleStore) DeletePrefix(prefix string) { delete(cs, prefix) - - /*if err := dumper.deleteAll(prefix); err != nil { - utils.Logger.Info(" 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 -} diff --git a/cache2go/lruttl.go b/cache2go/lruttl.go index 6603fb571..0afa04982 100644 --- a/cache2go/lruttl.go +++ b/cache2go/lruttl.go @@ -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) } diff --git a/cache2go/lruttl_test.go b/cache2go/lruttl_test.go index 47bbc8ae9..4f459d7e4 100644 --- a/cache2go/lruttl_test.go +++ b/cache2go/lruttl_test.go @@ -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()) } diff --git a/engine/storage_test.go b/engine/storage_test.go index 097b1fd6b..38c603c6e 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -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)