Skip to content

Commit

Permalink
change sessioncache to use bigcache
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblakeley committed Mar 13, 2019
1 parent 0edfb00 commit a6502c4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
5 changes: 3 additions & 2 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"strings"
"time"

cache "github.com/pmylund/go-cache"
"github.com/allegro/bigcache"

"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/request"
"github.com/TykTechnologies/tyk/user"
"github.com/pmylund/go-cache"
)

// Enums for keys to be stored in a session context - this is how gorilla expects
Expand Down Expand Up @@ -49,7 +50,7 @@ const (

var (
// key session memory cache
SessionCache = cache.New(10*time.Second, 5*time.Second)
SessionCache, _ = bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Second))

// org session memory cache
ExpiryCache = cache.New(600*time.Second, 10*time.Minute)
Expand Down
17 changes: 11 additions & 6 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -224,7 +225,8 @@ func (t BaseMiddleware) UpdateRequestSession(r *http.Request) bool {
ctxDisableSessionUpdate(r)

if !t.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
SessionCache.Set(session.KeyHash(), *session, cache.DefaultExpiration)
b, _ := json.Marshal(*session)
SessionCache.Set(session.KeyHash(), b)
}

return true
Expand Down Expand Up @@ -433,10 +435,11 @@ func (t BaseMiddleware) CheckSessionAndIdentityForValidKey(key string, r *http.R

// Check in-memory cache
if !t.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
cachedVal, found := SessionCache.Get(cacheKey)
if found {
cachedVal, err := SessionCache.Get(cacheKey)
if err == nil {
t.Logger().Debug("--> Key found in local cache")
session := cachedVal.(user.SessionState)
var session user.SessionState
json.Unmarshal(cachedVal, session)
if err := t.ApplyPolicies(&session); err != nil {
t.Logger().Error(err)
return session, false
Expand All @@ -453,7 +456,8 @@ func (t BaseMiddleware) CheckSessionAndIdentityForValidKey(key string, r *http.R
// If exists, assume it has been authorized and pass on
// cache it
if !t.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
go SessionCache.Set(cacheKey, session, cache.DefaultExpiration)
b, _ := json.Marshal(session)
SessionCache.Set(cacheKey, b)
}

// Check for a policy, if there is a policy, pull it and overwrite the session values
Expand All @@ -475,7 +479,8 @@ func (t BaseMiddleware) CheckSessionAndIdentityForValidKey(key string, r *http.R

// cache it
if !t.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
go SessionCache.Set(cacheKey, session, cache.DefaultExpiration)
b, _ := json.Marshal(session)
SessionCache.Set(cacheKey, b)
}

// Check for a policy, if there is a policy, pull it and overwrite the session values
Expand Down
5 changes: 4 additions & 1 deletion mw_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,11 @@ func (k *JWTMiddleware) processCentralisedJWT(r *http.Request, token *jwt.Token)
session.Expires = int64(f)
}
}
b, _ := json.Marshal(session)
go func() {
SessionCache.Set(session.KeyHash(), b)
}()

go SessionCache.Set(session.KeyHash(), session, cache.DefaultExpiration)
}
}

Expand Down
17 changes: 10 additions & 7 deletions mw_organisation_activity.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package main

import (
"encoding/json"
"net/http"
"sync"

"errors"

"time"

"github.com/TykTechnologies/tyk/request"
"github.com/TykTechnologies/tyk/user"
)
Expand Down Expand Up @@ -66,9 +65,11 @@ func (k *OrganizationMonitor) ProcessRequest(w http.ResponseWriter, r *http.Requ

// try to check in in-app cache 1st
if !k.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
var cachedSession interface{}
if cachedSession, found = SessionCache.Get(k.Spec.OrgID); found {
orgSession = cachedSession.(user.SessionState)
var session user.SessionState
cachedSession, err := SessionCache.Get(k.Spec.OrgID)
if err == nil {
json.Unmarshal(cachedSession, session)
orgSession = session
}
}

Expand Down Expand Up @@ -120,7 +121,8 @@ func (k *OrganizationMonitor) ProcessRequestLive(r *http.Request, orgSession use
if err := k.Spec.OrgSessionManager.UpdateSession(k.Spec.OrgID, &orgSession, sessionLifeTime, false); err == nil {
// update in-app cache if needed
if !k.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
SessionCache.Set(k.Spec.OrgID, orgSession, time.Second*time.Duration(sessionLifeTime))
b, _ := json.Marshal(orgSession)
SessionCache.Set(k.Spec.OrgID, b)
}
} else {
logger.WithError(err).Error("Could not update org session")
Expand Down Expand Up @@ -251,7 +253,8 @@ func (k *OrganizationMonitor) AllowAccessNext(
if err := k.Spec.OrgSessionManager.UpdateSession(k.Spec.OrgID, session, sessionLifeTime, false); err == nil {
// update in-app cache if needed
if !k.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
SessionCache.Set(k.Spec.OrgID, *session, time.Second*time.Duration(sessionLifeTime))
b, _ := json.Marshal(*session)
SessionCache.Set(k.Spec.OrgID, b)
}
} else {
logEntry.WithError(err).WithField("orgID", k.Spec.OrgID).Error("Could not update org session")
Expand Down

0 comments on commit a6502c4

Please sign in to comment.