Skip to content

Instantly share code, notes, and snippets.

@chmouel
Created October 26, 2012 19:23
Show Gist options
  • Save chmouel/3960888 to your computer and use it in GitHub Desktop.
Save chmouel/3960888 to your computer and use it in GitHub Desktop.
reuse swift memcache instead of spawning one
diff --git i/keystone/middleware/auth_token.py w/keystone/middleware/auth_token.py
index 5c198e8..8f0ceb3 100644
--- i/keystone/middleware/auth_token.py
+++ w/keystone/middleware/auth_token.py
@@ -237,23 +237,13 @@ class AuthProtocol(object):
# Token caching via memcache
self._cache = None
self._iso8601 = None
- memcache_servers = self._conf_get('memcache_servers')
# By default the token will be cached for 5 minutes
self.token_cache_time = int(self._conf_get('token_cache_time'))
self._token_revocation_list = None
self._token_revocation_list_fetched_time = None
cache_timeout = datetime.timedelta(seconds=0)
self.token_revocation_list_cache_timeout = cache_timeout
- if memcache_servers:
- try:
- import memcache
- import iso8601
- LOG.info('Using memcache for caching token')
- self._cache = memcache.Client(memcache_servers.split(','))
- self._iso8601 = iso8601
- except ImportError as e:
- LOG.warn('disabled caching due to missing libraries %s', e)
-
+
def _conf_get(self, name):
# try config from paste-deploy first
if name in self.conf:
@@ -268,6 +258,21 @@ class AuthProtocol(object):
we can't authenticate.
"""
+ memcache_servers = self._conf_get('memcache_servers')
+ if memcache_servers:
+ try:
+ import memcache
+ import iso8601
+ LOG.info('Using memcache for caching token')
+ self._iso8601 = iso8601
+ self._cache = memcache.Client(memcache_servers.split(','))
+ self._cache_swift = False
+ except ImportError as e:
+ LOG.warn('disabled caching due to missing libraries %s', e)
+
+ if env.get('swift.cache'):
+ self._cache = env.get('swift.cache')
+ self._cache_swift = True
LOG.debug('Authenticating user token')
try:
self._remove_auth_headers(env)
@@ -625,18 +630,24 @@ class AuthProtocol(object):
LOG.error('invalid token format')
return
LOG.debug('Storing %s token in memcache', token)
- self._cache.set(key,
- (data, expires),
- time=self.token_cache_time)
+ if self._cache_swift:
+ self._cache.set(key, (data, expires))
+ else:
+ self._cache.set(key,
+ (data, expires),
+ time=self.token_cache_time)
def _cache_store_invalid(self, token):
"""Store invalid token in cache."""
if self._cache:
key = 'tokens/%s' % token
LOG.debug('Marking token %s as unauthorized in memcache', token)
- self._cache.set(key,
- 'invalid',
- time=self.token_cache_time)
+ if self._cache_swift:
+ self._cache.set(key, 'invalid')
+ else:
+ self._cache.set(key,
+ 'invalid',
+ time=self.token_cache_time)
def cert_file_missing(self, called_proc_err, file_name):
return (called_proc_err.output.find(file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment