Commit a5ce6d80 authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: Session manager cache

parent cd392d95
Branches
Tags
2 merge requests!173Mep release19032020,!170Add: Functionality to call session manager from bls.
Showing with 89 additions and 0 deletions
package sessionmanagermdl
import (
"coresls/servers/coresls/app/models"
"errors"
"time"
"github.com/tidwall/gjson"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/cachemdl"
)
// Entry is a data to be stores against a key.
type Entry struct {
Data gjson.Result `json:"data,omitempty"`
Expiration int64 `json:"expiration,omitempty"`
ExpiredAT int64 `json:"expiredAt,omitempty"`
}
var store cachemdl.Cacher
var ErrSessionNotFound = errors.New("SESSION_NOT_FOUND")
var ErrInvalidDataType = errors.New("INVALID_DATA_Type")
// InitSessionManagerCache initializes the cache with provided configuration. Need to provide a cache type to use.
func InitSessionManagerCache(chacheType int) {
cacheConfig := cachemdl.CacheConfig{
Type: chacheType,
RedisCache: &cachemdl.RedisCache{
Addr: "",
DB: models.RedisDBSessionManager,
Prefix: models.ProjectID,
},
FastCache: &cachemdl.FastCacheHelper{
CleanupTime: 60 * time.Minute,
MaxEntries: 1000,
Expiration: -1,
},
}
store = cachemdl.GetCacheInstance(&cacheConfig)
}
func NewEntry(val gjson.Result, exp int64) Entry {
duration := time.Duration(exp) * time.Second
deadLine := time.Now().Add(duration).Unix()
return Entry{
Data: val,
Expiration: exp,
ExpiredAT: deadLine,
}
}
// Store adds/ updates the entry against the provided key.
func Store(key string, entry Entry) {
duration := time.Duration(entry.Expiration)
// deadLine := time.Now().Add(duration).Unix()
// entry := Entry{
// Data: val,
// Expiration: exp,
// ExpiredAT: deadLine,
// }
store.SetWithExpiration(key, entry, duration)
}
// Retrive returns the entry present against the provided key. If a key is not available or data stored is not of type gjson.Result, a non nil error will be returned
func Retrive(key string) (Entry, error) {
data, ok := store.Get(key)
if !ok {
return Entry{}, ErrSessionNotFound
}
entry, ok := data.(Entry)
if !ok {
return Entry{}, ErrInvalidDataType
}
return entry, nil
}
func Delete(key string) error {
_, ok := store.Get(key)
if !ok {
return ErrSessionNotFound
}
store.Delete(key)
return nil
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment