load all destination in cache if more than 0.5

This commit is contained in:
Radu Ioan Fericean
2014-11-27 12:06:47 +02:00
parent 4735c7c0a4
commit dac0248c15
4 changed files with 11 additions and 10 deletions

View File

@@ -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)

View File

@@ -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]
}

View File

@@ -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)

View File

@@ -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()