Skip to content

Instantly share code, notes, and snippets.

@pferreir
Created February 3, 2015 09:09
Show Gist options
  • Save pferreir/ec4c9744dd02be6f99a7 to your computer and use it in GitHub Desktop.
Save pferreir/ec4c9744dd02be6f99a7 to your computer and use it in GitHub Desktop.
diff --git a/indico/MaKaC/common/cache.py b/indico/MaKaC/common/cache.py
index dfce4ff..d4203f8 100644
--- a/indico/MaKaC/common/cache.py
+++ b/indico/MaKaC/common/cache.py
@@ -471,13 +471,28 @@ class FileCacheClient(CacheClient):
class GenericCache(object):
- def __init__(self, namespace):
+ def __init__(self, namespace, force_backend=None):
self._client = None
self._namespace = namespace
+ if force_backend:
+ self._client = self._select_backend(force_backend)
+
def __repr__(self):
return 'GenericCache(%r)' % self._namespace
+ def _select_backend(self, backend):
+ # If not, create a new one
+ if backend == 'memcached':
+ import memcache
+ return memcache.Client(Config.getInstance().getMemcachedServers())
+ elif backend == 'redis':
+ return RedisCacheClient(Config.getInstance().getRedisCacheURL())
+ elif backend == 'files':
+ return FileCacheClient(Config.getInstance().getXMLCacheDir())
+ else:
+ return NullCacheClient()
+
def _connect(self):
# Maybe we already have a client in this instance
if self._client is not None:
@@ -488,17 +503,7 @@ class GenericCache(object):
if self._client is not None:
return
- # If not, create a new one
- backend = Config.getInstance().getCacheBackend()
- if backend == 'memcached':
- import memcache
- self._client = memcache.Client(Config.getInstance().getMemcachedServers())
- elif backend == 'redis':
- self._client = RedisCacheClient(Config.getInstance().getRedisCacheURL())
- elif backend == 'files':
- self._client = FileCacheClient(Config.getInstance().getXMLCacheDir())
- else:
- self._client = NullCacheClient()
+ self._client = self._select_backend(Config.getInstance().getCacheBackend())
ContextManager.set('GenericCacheClient', self._client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment