From d05cd92a8d8957ad588e843e2577b17d85239960 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 15 Nov 2013 14:12:20 +0200 Subject: [PATCH] added RemKey method for cache --- apier/v1/apier.go | 7 +++---- cache2go/cache.go | 17 +++++++++++++++++ engine/storage_interface.go | 2 +- engine/storage_redis.go | 13 ++++++------- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 615b8d0ca..628fa4103 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -322,19 +322,18 @@ func (self *ApierV1) ReloadCache(attrs utils.ApiReloadCache, reply *string) erro if len(attrs.DestinationIds) > 0 { dstKeys = make([]string, len(attrs.DestinationIds)) for idx, dId := range attrs.DestinationIds { - dstKeys[idx] = engine.DESTINATION_PREFIX+dId // Cache expects them as redis keys + dstKeys[idx] = engine.DESTINATION_PREFIX + dId // Cache expects them as redis keys } } if len(attrs.RatingPlanIds) > 0 { rpKeys = make([]string, len(attrs.RatingPlanIds)) for idx, rpId := range attrs.RatingPlanIds { - rpKeys[idx] = engine.RATING_PLAN_PREFIX+rpId + rpKeys[idx] = engine.RATING_PLAN_PREFIX + rpId } } - if err := self.DataDb.PreCache(dstKeys, rpKeys); err!= nil { + if err := self.DataDb.PreCache(dstKeys, rpKeys); err != nil { return err } *reply = "OK" return nil } - diff --git a/cache2go/cache.go b/cache2go/cache.go index 03b79b647..9b7e6d2b8 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -104,6 +104,23 @@ func GetCached(key string) (v interface{}, err error) { return nil, errors.New("not found") } +func RemKey(key string) { + mux.Lock() + defer mux.Unlock() + delete(cache, key) +} + +func XRemKey(key string) { + mux.Lock() + defer mux.Unlock() + if r, ok := xcache[key]; ok { + if r.timer() != nil { + r.timer().Stop() + } + } + delete(cache, key) +} + // Delete all keys from expiraton cache func XFlush() { xMux.Lock() diff --git a/engine/storage_interface.go b/engine/storage_interface.go index c24851b27..17c61e262 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -63,7 +63,7 @@ Interface for storage providers. type DataStorage interface { Storage PreCache([]string, []string) error - ExistsData(string, string)(bool, error) + ExistsData(string, string) (bool, error) GetRatingPlan(string) (*RatingPlan, error) SetRatingPlan(*RatingPlan) error GetRatingProfile(string) (*RatingProfile, error) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 1fb9a531d..df9206088 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -21,6 +21,7 @@ package engine import ( "bytes" "compress/zlib" + "errors" "fmt" "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/history" @@ -30,7 +31,6 @@ import ( "strconv" "strings" "time" - "errors" ) type RedisStorage struct { @@ -88,7 +88,7 @@ func (rs *RedisStorage) PreCache(dKeys, rpKeys []string) (err error) { } } for _, key := range dKeys { - // ToDo: cache2go.RemKey(key) + cache2go.RemKey(key) if _, err = rs.GetDestination(key[len(DESTINATION_PREFIX):]); err != nil { return err } @@ -99,7 +99,7 @@ func (rs *RedisStorage) PreCache(dKeys, rpKeys []string) (err error) { } } for _, key := range rpKeys { - // ToDo: cache2go.RemKey(key) + cache2go.RemKey(key) if _, err = rs.GetRatingPlan(key[len(RATING_PLAN_PREFIX):]); err != nil { return err } @@ -110,15 +110,14 @@ func (rs *RedisStorage) PreCache(dKeys, rpKeys []string) (err error) { // Used to check if specific subject is stored using prefix key attached to entity func (rs *RedisStorage) ExistsData(entity, subject string) (bool, error) { switch entity { - case DESTINATION_PREFIX: - return rs.db.Exists(DESTINATION_PREFIX+subject) + case DESTINATION_PREFIX: + return rs.db.Exists(DESTINATION_PREFIX + subject) case RATING_PLAN_PREFIX: - return rs.db.Exists(RATING_PLAN_PREFIX+subject) + return rs.db.Exists(RATING_PLAN_PREFIX + subject) } return false, errors.New("Unsupported entity in ExistsData") } - func (rs *RedisStorage) GetRatingPlan(key string) (rp *RatingPlan, err error) { if x, err := cache2go.GetCached(key); err == nil { return x.(*RatingPlan), nil