mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Removing golang-lru dependency, init cache to consider default configuration
This commit is contained in:
5
cache/cache.go
vendored
5
cache/cache.go
vendored
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
83
cache/cache_store.go
vendored
83
cache/cache_store.go
vendored
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
2
cache/cache_test.go
vendored
2
cache/cache_test.go
vendored
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
4
glide.lock
generated
4
glide.lock
generated
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user