added caching for activation periods

This commit is contained in:
Radu Ioan Fericean
2012-08-14 17:56:12 +03:00
parent 9f0203dffe
commit 271ce66786
4 changed files with 25 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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.
*/

View File

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

View File

@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package timespans
import (
"github.com/rif/cache"
"github.com/rif/cache2go"
"strings"
)

View File

@@ -20,7 +20,7 @@ package timespans
import (
"encoding/json"
"github.com/rif/cache"
"github.com/rif/cache2go"
"reflect"
"testing"
)