From 76db6a03f2948599be0875d3df2def6f73fe65e0 Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 24 Jul 2017 19:02:17 +0200 Subject: [PATCH] Removing golang-lru dependency, init cache to consider default configuration --- cache/cache.go | 5 +-- cache/cache_store.go | 83 ++++---------------------------------------- cache/cache_test.go | 2 +- glide.lock | 4 --- glide.yaml | 1 - 5 files changed, 11 insertions(+), 84 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 8df7a91b4..b4fa5233d 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -53,7 +53,8 @@ type transactionItem struct { } func init() { - NewCache(nil) + dfCfg, _ := config.NewDefaultCGRConfig() + NewCache(dfCfg.CacheConfig) } func NewCache(cacheCfg config.CacheConfig) { @@ -146,7 +147,7 @@ func RemPrefixKey(prefix string, commit bool, transID string) { // Delete all keys from cache func Flush() { cacheMux.Lock() - cache = newLruStore() + cache.Clear() cacheMux.Unlock() } diff --git a/cache/cache_store.go b/cache/cache_store.go index 2dbaa719a..16c32af46 100644 --- a/cache/cache_store.go +++ b/cache/cache_store.go @@ -23,7 +23,6 @@ import ( "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" "github.com/cgrates/ltcache" - lru "github.com/hashicorp/golang-lru" ) type cacheStore interface { @@ -33,81 +32,7 @@ type cacheStore interface { DeletePrefix(string) CountEntriesForPrefix(string) int GetKeysForPrefix(string) []string -} - -// easy to be counted exported by prefix -type lrustore map[string]*lru.Cache - -func newLruStore() (c lrustore) { - c = make(map[string]*lru.Cache) - c[utils.ANY], _ = lru.New(0) - if cfg == nil { - return - } - // dynamically configure cache instances based on CacheConfig - for cfgKey := range cfg { - cacheInstanceID := cfgKey - if prefixKey, has := utils.CacheInstanceToPrefix[cfgKey]; has { - cacheInstanceID = prefixKey // old aliases, backwards compatibility purpose - } - c[cacheInstanceID], _ = lru.New(cfg[cfgKey].Limit) - } - return -} - -func (cs lrustore) Put(key string, value interface{}) { - prefix, key := key[:PREFIX_LEN], key[PREFIX_LEN:] - mp, ok := cs[prefix] - if !ok { - var err error - mp, err = lru.New(10000) - if err != nil { - return - } - cs[prefix] = mp - } - mp.Add(key, value) -} - -func (cs lrustore) Get(key string) (interface{}, bool) { - prefix, key := key[:PREFIX_LEN], key[PREFIX_LEN:] - if keyMap, ok := cs[prefix]; ok { - if ti, exists := keyMap.Get(key); exists { - return ti, true - } - } - return nil, false -} - -func (cs lrustore) Delete(key string) { - prefix, key := key[:PREFIX_LEN], key[PREFIX_LEN:] - if keyMap, ok := cs[prefix]; ok { - keyMap.Remove(key) - } -} - -func (cs lrustore) DeletePrefix(prefix string) { - delete(cs, prefix) -} - -func (cs lrustore) CountEntriesForPrefix(prefix string) int { - if m, ok := cs[prefix]; ok { - return m.Len() - } - return 0 -} - -func (cs lrustore) GetKeysForPrefix(prefix string) (keys []string) { - prefix, key := prefix[:PREFIX_LEN], prefix[PREFIX_LEN:] - if keyMap, ok := cs[prefix]; ok { - for _, iterKey := range keyMap.Keys() { - iterKeyString := iterKey.(string) - if len(key) == 0 || strings.HasPrefix(iterKeyString, key) { - keys = append(keys, prefix+iterKeyString) - } - } - } - return + Clear() } type cacheLRUTTL map[string]*ltcache.Cache @@ -175,3 +100,9 @@ func (cs cacheLRUTTL) GetKeysForPrefix(prefix string) (keys []string) { } return } + +func (cs cacheLRUTTL) Clear() { + for _, cInst := range cs { + cInst.Clear() + } +} diff --git a/cache/cache_test.go b/cache/cache_test.go index 11a2a950e..459b434f0 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -136,7 +136,7 @@ func TestCacheCount(t *testing.T) { Set("rpf_A3", "3", true, "") Set("dst_A4", "4", true, "") Set("dst_A5", "5", true, "") - if cnt := CountEntries(utils.ANY); cnt != 5 { + if cnt := CountEntries(utils.DESTINATION_PREFIX); cnt != 4 { t.Error("Error counting entries: ", cnt) } } diff --git a/glide.lock b/glide.lock index 661bab955..edd14a20a 100644 --- a/glide.lock +++ b/glide.lock @@ -59,10 +59,6 @@ imports: version: 0b58b37b664c21f3010e836f1b931e1d0b0b0685 - name: github.com/gorhill/cronexpr version: f0984319b44273e83de132089ae42b1810f4933b -- name: github.com/hashicorp/golang-lru - version: 0a025b7e63adc15a622f29b0b2c4c3848243bbf6 - subpackages: - - simplelru - name: github.com/jinzhu/gorm version: 0f2ceb5a775714a46bc344976324e3e439f8cdcc - name: github.com/jinzhu/inflection diff --git a/glide.yaml b/glide.yaml index f9347d4e8..d8aa1ca45 100644 --- a/glide.yaml +++ b/glide.yaml @@ -36,7 +36,6 @@ import: - bson - package: github.com/ChrisTrenkamp/goxpath - package: github.com/mitchellh/mapstructure -- package: github.com/hashicorp/golang-lru - package: github.com/cgrates/aringo - package: github.com/bit4bit/gami - package: github.com/streadway/amqp