diff --git a/authmdl/sessionmdl/session.go b/authmdl/sessionmdl/session.go index 207cc7ff90ccabbeba4fbfb82d9c38c047809094..87523ed2b8975e229a81f500c21a5175bcc8b3aa 100644 --- a/authmdl/sessionmdl/session.go +++ b/authmdl/sessionmdl/session.go @@ -52,6 +52,15 @@ func InitUserSessionCache(chacheType int) { store = cachemdl.GetCacheInstance(&cacheConfig) } +// Init initializes sessions with provided cache. Subsequent calls will not have any effect after first initialization. +func Init(cache cachemdl.Cacher) { + if store != nil { + return + } + + store = cache +} + // Set stores the sessions for provided userId. Session is appended to the list. It does not check if the same session exists or not. func Set(userId string, s ...Session) { i, ok := store.Get(userId) diff --git a/authmdl/sessionmdl/session_test.go b/authmdl/sessionmdl/session_test.go index 7e207adcaf0414a657328cf12b3924129804d9a3..fb27c67a2d8f2bd801f772bfcc0c651379121e53 100644 --- a/authmdl/sessionmdl/session_test.go +++ b/authmdl/sessionmdl/session_test.go @@ -7,7 +7,7 @@ import ( ) func init() { - InitUserSessionCache(cachemdl.TypeFastCache) + Init(cachemdl.SetupFastCache(cachemdl.FCWithMaxEntries(10000))) } func TestSet(t *testing.T) { diff --git a/cachemdl/cache_redis.go b/cachemdl/cache_redis.go index a021c28a6a8b43400d46fbfa25aa26a207744855..0f532229eba3109e59aa94c0db6479bf8759f656 100644 --- a/cachemdl/cache_redis.go +++ b/cachemdl/cache_redis.go @@ -16,6 +16,7 @@ Note - corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl must import ( "encoding/json" + "errors" "log" "strings" "time" @@ -51,6 +52,42 @@ type RedisCache struct { Prefix string // this will be used for storing keys for provided project } +type configRedis struct { + addr string // redis server address, default "127.0.0.1:6379" + db int // redis DB on provided server, default 0 + password string // + expiration time.Duration // this duration will be used for Set() method + prefix string // this will be used for storing keys for provided project +} + +type redisOption func(*configRedis) + +func RedisWithAddr(addr string) redisOption { + return func(cfg *configRedis) { + cfg.addr = addr + } +} +func RedisWithDB(db int) redisOption { + return func(cfg *configRedis) { + cfg.db = db + } +} +func RedisWithPrefix(pfx string) redisOption { + return func(cfg *configRedis) { + cfg.prefix = pfx + } +} +func RedisWithPassword(p string) redisOption { + return func(cfg *configRedis) { + cfg.password = p + } +} +func RedisWithExpiration(exp time.Duration) redisOption { + return func(cfg *configRedis) { + cfg.expiration = exp + } +} + // Setup initializes redis cache for application. Must be called only once. func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Duration) { @@ -87,6 +124,46 @@ func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Dura } +// SetupRedisCache initializes redis cache for application and returns it. Must be called only once. +func SetupRedisCache(opts ...redisOption) (*RedisCache, error) { + + rc := new(RedisCache) + + cfg := new(configRedis) + + for i := range opts { + opts[i](cfg) + } + + rc.Addr = cfg.addr + rc.Password = cfg.password + rc.DB = cfg.db + rc.Expiration = cfg.expiration + rc.Prefix = cfg.prefix + + rc.opt = &redis.Options{ + Addr: cfg.addr, + Password: cfg.password, + DB: cfg.db, + } + + rc.cli = redis.NewClient(rc.opt) + + if _, err := rc.cli.Ping().Result(); err != nil { + + return nil, errors.New("connection to redis server failed: " + err.Error()) + } + + rc.connected = true + + if cfg.prefix != "" { + rc.keyStr = contcat(rc.Prefix, keySplitter) + rc.addPrefix = true + } + + return rc, nil +} + // Set marshalls provided value and stores against provided key. Errors will be logged to initialized logger. func (rc *RedisCache) Set(key string, val interface{}) { ba, err := marshalWithTypeCheck(val) diff --git a/cachemdl/cachemdl.go b/cachemdl/cachemdl.go index 5d15698eb9f11e1918fb6291148f3d123d562b11..f2eb3f89a8c41d47548655faa127333a6cee4c42 100755 --- a/cachemdl/cachemdl.go +++ b/cachemdl/cachemdl.go @@ -19,6 +19,26 @@ type FastCacheHelper struct { MaxEntries int } +type fastCacheOption func(*FastCacheHelper) + +func FCWithMaxEntries(i int) fastCacheOption { + return func(cfg *FastCacheHelper) { + cfg.MaxEntries = i + } +} + +func FCWithExpiration(exp time.Duration) fastCacheOption { + return func(cfg *FastCacheHelper) { + cfg.Expiration = exp + } +} + +func FCWithCleanupInterval(ivl time.Duration) fastCacheOption { + return func(cfg *FastCacheHelper) { + cfg.CleanupTime = ivl + } +} + // Setup initializes fastcache cache for application. Must be called only once. func (fastCacheHelper *FastCacheHelper) Setup(maxEntries int, expiration time.Duration, cleanupTime time.Duration) { @@ -28,6 +48,18 @@ func (fastCacheHelper *FastCacheHelper) Setup(maxEntries int, expiration time.Du } +// SetupFastCache initializes fastcache cache for application and returns its instance. +func SetupFastCache(opts ...fastCacheOption) *FastCacheHelper { + fc := new(FastCacheHelper) + + for i := range opts { + opts[i](fc) + } + + fc.FastCache = cache.New(fc.Expiration, fc.CleanupTime) + return fc +} + // Get - func (fastCacheHelper *FastCacheHelper) Get(key string) (interface{}, bool) { return fastCacheHelper.FastCache.Get(key) diff --git a/sessionmanagermdl/sessionmanager.go b/sessionmanagermdl/sessionmanager.go index bcde051d05354001266697e755abac7ca43940e7..937bb98783de4d47871059d4c8aad78ba4bb4f01 100644 --- a/sessionmanagermdl/sessionmanager.go +++ b/sessionmanagermdl/sessionmanager.go @@ -49,6 +49,15 @@ func InitSessionManagerCache(chacheType int) { store = cachemdl.GetCacheInstance(&cacheConfig) } +// Init initializes session manager with provided cache. Subsequent calls will not have any effect after first initialization. +func Init(cache cachemdl.Cacher) { + if store != nil { + return + } + + store = cache +} + // NewEntry prepares the object required to store data in session. // // The `exp` field interprets time in seconds. Ex. For 5 seconds, set `5`