From 271ce6678617520268d0970798cec4c625ec0961 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 14 Aug 2012 17:56:12 +0300 Subject: [PATCH] added caching for activation periods --- timespans/activationperiod.go | 7 +++++++ timespans/calldesc.go | 16 ++++++++++++++++ timespans/destinations.go | 2 +- timespans/destinations_test.go | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/timespans/activationperiod.go b/timespans/activationperiod.go index 173f3a55c..70e3ac0fc 100644 --- a/timespans/activationperiod.go +++ b/timespans/activationperiod.go @@ -19,6 +19,7 @@ along with this program. If not, see package timespans import ( + "github.com/rif/cache2go" "strconv" "strings" "time" @@ -32,6 +33,12 @@ type ActivationPeriod struct { Intervals []*Interval } +type xCachedActivationPeriods struct { + destPrefix string + aps []*ActivationPeriod + *cache.XEntry +} + /* Adds one ore more intervals to the internal interval list. */ diff --git a/timespans/calldesc.go b/timespans/calldesc.go index 7641cfbe4..39f358acf 100644 --- a/timespans/calldesc.go +++ b/timespans/calldesc.go @@ -21,6 +21,7 @@ package timespans import ( "errors" "fmt" + "github.com/rif/cache2go" "log/syslog" "math" "time" @@ -44,6 +45,7 @@ const ( var ( storageGetter, _ = NewMapStorage() + debitPeriod = 10 * time.Second //storageGetter, _ = NewMongoStorage("localhost", "cgrates") //storageGetter, _ = NewRedisStorage("127.0.0.1:6379", 10, "") Logger LoggerInterface @@ -128,10 +130,22 @@ func SetStorageGetter(sg StorageGetter) { storageGetter = sg } +/* +Exported method to set the debit period for caching purposes. +*/ +func SetDebitPeriod(d time.Duration) { + debitPeriod = d +} + /* Restores the activation periods for the specified prefix from storage. */ func (cd *CallDescriptor) SearchStorageForPrefix() (destPrefix string, err error) { + if val, err := cache.GetXCached(cd.GetKey()); err == nil { + xaps := val.(xCachedActivationPeriods) + cd.ActivationPeriods = xaps.aps + return xaps.destPrefix, nil + } cd.ActivationPeriods = make([]*ActivationPeriod, 0) base := fmt.Sprintf("%s:%s:%s:%s:", cd.Direction, cd.Tenant, cd.TOR, cd.Subject) destPrefix = cd.Destination @@ -155,6 +169,8 @@ func (cd *CallDescriptor) SearchStorageForPrefix() (destPrefix string, err error } //load the activation preriods if err == nil && len(values) > 0 { + xaps := xCachedActivationPeriods{destPrefix, values, new(cache.XEntry)} + xaps.XCache(cd.GetKey(), debitPeriod+5*time.Second, xaps) cd.ActivationPeriods = values } return diff --git a/timespans/destinations.go b/timespans/destinations.go index e56fc2702..8d5a4c9dd 100644 --- a/timespans/destinations.go +++ b/timespans/destinations.go @@ -19,7 +19,7 @@ along with this program. If not, see package timespans import ( - "github.com/rif/cache" + "github.com/rif/cache2go" "strings" ) diff --git a/timespans/destinations_test.go b/timespans/destinations_test.go index 9f6cdba2a..09805c885 100644 --- a/timespans/destinations_test.go +++ b/timespans/destinations_test.go @@ -20,7 +20,7 @@ package timespans import ( "encoding/json" - "github.com/rif/cache" + "github.com/rif/cache2go" "reflect" "testing" )