-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support explicit ttl #93
base: master
Are you sure you want to change the base?
Changes from 1 commit
572d85f
7a2e2ea
b2c1614
0956e0b
869b477
8e4e81a
c01d20c
07fdd33
439f133
7d0c3c0
ec23b31
42e5bc5
9f127ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,10 @@ map.setup({ | |
purgeInterval: 10000 | ||
}); | ||
|
||
var tokenCacheSize = 100; | ||
var tokenCacheSize = 100; //default cache size for access tokens | ||
var tokenCacheTTL = 60000; //set default token cache TTL to 1 minute | ||
var cacheKeyTTL = 60000; //set default apikey cache TTL to 1 minute | ||
var cacheSize = 100; //default cache size for api keys | ||
|
||
module.exports.init = function(config, logger, stats) { | ||
|
||
|
@@ -43,6 +46,10 @@ module.exports.init = function(config, logger, stats) { | |
var apiKeyHeaderName = config.hasOwnProperty('api-key-header') ? config['api-key-header'] : 'x-api-key'; | ||
var keepAuthHeader = config.hasOwnProperty('keep-authorization-header') ? config['keep-authorization-header'] : false; | ||
cacheKey = config.hasOwnProperty('cacheKey') ? config.cacheKey : false; | ||
//cache ttl | ||
cacheKeyTTL = config.hasOwnProperty("cacheKeyTTL") ? config.cacheKeyTTL : 60000; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't respect the default cacheKeyTTL value prev set on line 35 - avoid magic numbers. same for other cache vars |
||
//cache size | ||
cacheSize = config.hasOwnProperty("cacheSize") ? config.cacheSize : 100; | ||
//set grace period | ||
var gracePeriod = config.hasOwnProperty('gracePeriod') ? config.gracePeriod : 0; | ||
acceptField.gracePeriod = gracePeriod; | ||
|
@@ -59,6 +66,8 @@ module.exports.init = function(config, logger, stats) { | |
} | ||
//token cache settings | ||
tokenCache = config.hasOwnProperty('tokenCache') ? config.tokenCache : false; | ||
//token cache ttl | ||
tokenCacheTTL = config.hasOwnProperty("tokenCacheTTL") ? config.cacheKeyTTL : 60000; | ||
//max number of tokens in the cache | ||
tokenCacheSize = config.hasOwnProperty('tokenCacheSize') ? config.tokenCacheSize : 100; | ||
// | ||
|
@@ -198,6 +207,7 @@ module.exports.init = function(config, logger, stats) { | |
var isValid = false; | ||
var oauthtoken = token && token.token ? token.token : token; | ||
var decodedToken = JWS.parse(oauthtoken); | ||
|
||
if (tokenCache == true) { | ||
debug('token caching enabled') | ||
map.read(oauthtoken, function(err, tokenvalue) { | ||
|
@@ -230,8 +240,8 @@ module.exports.init = function(config, logger, stats) { | |
} else { | ||
if (tokenvalue == null || tokenvalue == undefined) { | ||
map.size(function(err, sizevalue) { | ||
if (!err && sizevalue != null && sizevalue < 100) { | ||
map.store(oauthtoken, oauthtoken); | ||
if (!err && sizevalue != null && sizevalue < tokenCacheSize) { | ||
map.store(oauthtoken, oauthtoken, tokenCacheTTL); | ||
} else { | ||
debug('too many tokens in cache; ignore storing token'); | ||
} | ||
|
@@ -288,8 +298,13 @@ module.exports.init = function(config, logger, stats) { | |
// default to now (in seconds) + 30m if not set | ||
decodedToken.exp = decodedToken.exp || +(((Date.now() / 1000) + 1800).toFixed(0)); | ||
//apiKeyCache[apiKey] = decodedToken; | ||
cache.store(apiKey, decodedToken); | ||
debug('api key cache store', apiKey); | ||
cache.size(function(err, sizevalue) { | ||
if (!err && sizevalue != null && sizevalue < cacheSize) { | ||
cache.store(apiKey, decodedToken, cacheKeyTTL); | ||
} else { | ||
debug('too many keys in cache; ignore storing token'); | ||
} | ||
}); | ||
} else { | ||
debug('api key cache skip', apiKey); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,8 @@ map.setup({ | |
purgeInterval: 10000 | ||
}); | ||
|
||
var tokenCacheSize = 100; | ||
var tokenCacheSize = 100; //default cache size for access tokens | ||
var tokenCacheTTL = 60000; //set default token cache TTL to 1 minute | ||
|
||
module.exports.init = function(config, logger, stats) { | ||
|
||
|
@@ -50,6 +51,8 @@ module.exports.init = function(config, logger, stats) { | |
} | ||
//token cache settings | ||
tokenCache = config.hasOwnProperty('tokenCache') ? config.tokenCache : false; | ||
//token cache ttl | ||
tokenCacheTTL = config.hasOwnProperty("tokenCacheTTL") ? config.cacheKeyTTL : 60000; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't respect the default tokenCacheTTL value prev set above avoid magic numbers. same for other cache vars |
||
//max number of tokens in the cache | ||
tokenCacheSize = config.hasOwnProperty('tokenCacheSize') ? config.tokenCacheSize : 100; | ||
// | ||
|
@@ -129,8 +132,8 @@ module.exports.init = function(config, logger, stats) { | |
} else { | ||
if (tokenvalue == null || tokenvalue == undefined) { | ||
map.size(function(err, sizevalue) { | ||
if (!err && sizevalue != null && sizevalue < 100) { | ||
map.store(oauthtoken, oauthtoken); | ||
if (!err && sizevalue != null && sizevalue < tokenCacheSize) { | ||
map.store(oauthtoken, oauthtoken, tokenCacheTTL); | ||
} else { | ||
debug('too many tokens in cache; ignore storing token'); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't respect the default cacheKeyTTL value prev set on line 25 - avoid magic numbers. same for cacheSize