diff options
author | Tres Seaver <tseaver@palladion.com> | 2012-07-05 09:08:26 -0400 |
---|---|---|
committer | Tres Seaver <tseaver@palladion.com> | 2012-07-05 09:08:26 -0400 |
commit | 0913e85f644744e1780c90dde5ea087912a4d6dc (patch) | |
tree | 1680363019c5f0bf9ad180b1d4f9e0659b24d998 | |
parent | cc4dd32bab5f8d508c1cd610cff682f0531150b1 (diff) | |
download | repoze-lru-0913e85f644744e1780c90dde5ea087912a4d6dc.tar.gz |
Code clarity.
-rw-r--r-- | repoze/lru/__init__.py | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/repoze/lru/__init__.py b/repoze/lru/__init__.py index c082607..92de186 100644 --- a/repoze/lru/__init__.py +++ b/repoze/lru/__init__.py @@ -306,30 +306,30 @@ class CacheMaker(object): - timeout : the defaut size for the cache if using expriring cache """ self._maxsize = default.get("maxsize") - self._timeout = default.get("timeout") + self._timeout = default.get("timeout", _DEFAULT_TIMEOUT) self._cache = {} - def _resolve_setting(self, option): - name = option.get("name") - maxsize = option.get("maxsize") - maxsize = self._maxsize if maxsize is None else maxsize + def _resolve_setting(self, options): + name = options.get("name") + + if name is None: + while True: + name = str(uuid.uuid4()) + ## the probability of collision is so low .... + if name not in self._cache: + break + + if name in self._cache: + raise KeyError("cache %s already in use" % name) + + maxsize = options.get("maxsize", self._maxsize) if maxsize is None: raise ValueError("Cache must have a maxsize set") - timeout = option.get("timeout") - timeout = self._timeout if timeout is None else timeout - if name is None: - _name= str(uuid.uuid4()) - ## the probability of collision is so low .... - while _name in self._cache.keys(): - _name = str(uuid.uuid4()) #pragma NO COVER - else: - if name in self._cache: - raise KeyError("cache %s already in use" % name) - _name = name + timeout = options.get("timeout", self._timeout) - return dict(name=_name, timeout=timeout, maxsize=maxsize) + return dict(name=name, timeout=timeout, maxsize=maxsize) - def lrucache(self, **option): + def lrucache(self, **options): """Named arguments: - name (optional) is a string, and should be unique amongst all caches @@ -337,11 +337,11 @@ class CacheMaker(object): - maxsize (optional) is an int, overriding any default value set by the constructor """ - option = self._resolve_setting(option) - cache = self._cache[option["name"]] = LRUCache(option['maxsize']) - return lru_cache(option['maxsize'], cache) + options = self._resolve_setting(options) + cache = self._cache[options["name"]] = LRUCache(options['maxsize']) + return lru_cache(options['maxsize'], cache) - def expiring_lrucache(self, **option): + def expiring_lrucache(self, **options): """Named arguments: - name (optional) is a string, and should be unique amongst all caches @@ -352,17 +352,20 @@ class CacheMaker(object): - timeout (optional) is an int, overriding any default value set by the constructor or the default value (%d seconds) """ % _DEFAULT_TIMEOUT - option = self._resolve_setting(option) - cache = self._cache[option['name']] = ExpiringLRUCache( - option["maxsize"],option["timeout"] - ) - return lru_cache(option["maxsize"], cache, option["timeout"]) + options = self._resolve_setting(options) + cache = self._cache[options['name']] = ExpiringLRUCache( + options["maxsize"], options["timeout"]) + return lru_cache(options["maxsize"], cache, options["timeout"]) def clear(self, name=None): """Clear the given cache. If 'name' is not passed, clear all caches. """ - to_clear = self._cache.keys() if name is None else [ name ] + if name is None: + to_clear = self._cache.keys() + else: + to_clear = [name] + for cache_name in to_clear: self._cache[cache_name].clear() |