From dac0248c150e760a28abb2d86deb9bdb1d164145 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 27 Nov 2014 12:06:47 +0200 Subject: [PATCH] load all destination in cache if more than 0.5 --- cache2go/cache.go | 2 +- cache2go/store.go | 14 +++++++------- engine/storage_map.go | 2 +- engine/storage_redis.go | 3 ++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cache2go/cache.go b/cache2go/cache.go index 711b02125..43b0067bf 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -158,7 +158,7 @@ func Flush() { } } -func CountEntries(prefix string) (result int64) { +func CountEntries(prefix string) (result int) { mux.RLock() defer mux.RUnlock() return cache.CountEntriesForPrefix(prefix) diff --git a/cache2go/store.go b/cache2go/store.go index 9676005b6..0c6fd9880 100644 --- a/cache2go/store.go +++ b/cache2go/store.go @@ -16,7 +16,7 @@ type cacheStore interface { GetAge(string) (time.Duration, error) Delete(string) DeletePrefix(string) - CountEntriesForPrefix(string) int64 + CountEntriesForPrefix(string) int GetAllForPrefix(string) (map[string]timestampedValue, error) GetKeysForPrefix(string) []string } @@ -37,7 +37,7 @@ func (cs cacheDoubleStore) Put(key string, value interface{}) { } func (cs cacheDoubleStore) Append(key string, value interface{}) { - var elements map[interface{}]struct{} + var elements map[interface{}]struct{} // using map for faster check if element is present if v, err := cs.Get(key); err == nil { elements = v.(map[interface{}]struct{}) } else { @@ -81,9 +81,9 @@ func (cs cacheDoubleStore) DeletePrefix(prefix string) { delete(cs, prefix) } -func (cs cacheDoubleStore) CountEntriesForPrefix(prefix string) int64 { +func (cs cacheDoubleStore) CountEntriesForPrefix(prefix string) int { if _, ok := cs[prefix]; ok { - return int64(len(cs[prefix])) + return len(cs[prefix]) } return 0 } @@ -110,13 +110,13 @@ func (cs cacheDoubleStore) GetKeysForPrefix(prefix string) (keys []string) { // faster to access type cacheSimpleStore struct { cache map[string]timestampedValue - counters map[string]int64 + counters map[string]int } func newSimpleStore() cacheSimpleStore { return cacheSimpleStore{ cache: make(map[string]timestampedValue), - counters: make(map[string]int64), + counters: make(map[string]int), } } @@ -198,7 +198,7 @@ func (cs cacheSimpleStore) descount(key string) { } } -func (cs cacheSimpleStore) CountEntriesForPrefix(prefix string) int64 { +func (cs cacheSimpleStore) CountEntriesForPrefix(prefix string) int { if _, ok := cs.counters[prefix]; ok { return cs.counters[prefix] } diff --git a/engine/storage_map.go b/engine/storage_map.go index 55db2969f..575d5b1e2 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -58,7 +58,7 @@ func (ms *MapStorage) GetKeysForPrefix(string) ([]string, error) { func (ms *MapStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) error { cache2go.BeginTransaction() - if dKeys == nil { + if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*0.5 < float64(len(dKeys))) { cache2go.RemPrefixKey(DESTINATION_PREFIX) } else { CleanStalePrefixes(dKeys) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index ec6d14b67..207a17854 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -73,7 +73,8 @@ func (rs *RedisStorage) GetKeysForPrefix(prefix string) ([]string, error) { func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) (err error) { cache2go.BeginTransaction() - if dKeys == nil { + if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*0.5 < float64(len(dKeys))) { + // if need to load more than a half of exiting keys load them all Logger.Info("Caching all destinations") if dKeys, err = rs.db.Keys(DESTINATION_PREFIX + "*"); err != nil { cache2go.RollbackTransaction()