From 3a7da1ca3402580e008ed8cd81addd43287acd47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sat, 19 May 2012 23:35:39 +0200 Subject: Improved cache system --- sqlparse/utils.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 7 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/utils.py b/sqlparse/utils.py index 6321353..a349900 100644 --- a/sqlparse/utils.py +++ b/sqlparse/utils.py @@ -4,6 +4,60 @@ Created on 17/05/2012 @author: piranna ''' +try: + from collections import OrderedDict +except ImportError: + OrderedDict = None + + +if OrderedDict: + class Cache(OrderedDict): + """Cache with LRU algorithm using an OrderedDict as basis + """ + def __init__(self, maxsize=100): + OrderedDict.__init__(self) + + self._maxsize = maxsize + + def __getitem__(self, key, *args, **kwargs): + # Remove the (key, value) pair from the cache, or raise KeyError + value = self.pop(key) + + # Insert the (key, value) pair on the front of the cache + OrderedDict.__setitem__(self, key, value) + + # Return the value from the cache + return value + + def __setitem__(self, key, value, *args, **kwargs): + # Key was inserted before, remove it so we put it at front later + if key in self: + del self[key] + + # Too much items on the cache, remove the least recent used + elif len(self) >= self._maxsize: + self.popitem(False) + + # Insert the (key, value) pair on the front of the cache + OrderedDict.__setitem__(self, key, value, *args, **kwargs) + +else: + class Cache(dict): + """Cache that reset when gets full + """ + def __init__(self, maxsize=100): + dict.__init__(self) + + self._maxsize = maxsize + + def __setitem__(self, key, value, *args, **kwargs): + # Reset the cache if we have too much cached entries and start over + if len(self) >= self._maxsize: + self.clear() + + # Insert the (key, value) pair on the front of the cache + dict.__setitem__(self, key, value, *args, **kwargs) + def memoize_generator(func): """Memoize decorator for generators @@ -13,7 +67,7 @@ def memoize_generator(func): Obviusly, this is only useful if the generator will always return the same values for each specific parameters... """ - cache = {} + cache = Cache() def wrapped_func(*args, **kwargs): # params = (args, kwargs) @@ -26,12 +80,6 @@ def memoize_generator(func): # Not cached, exec and store it except KeyError: - # Reset the cache if we have too much cached entries and start over - # In the future would be better to use an OrderedDict and drop the - # Least Recent Used entries - if len(cache) >= 10: - cache.clear() - cached = [] for item in func(*args, **kwargs): -- cgit v1.2.1 From ba9f29071a4011521cfe685ba1de30004b413ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sat, 19 May 2012 23:44:24 +0200 Subject: I always forget to remove the prints for testing... :-P --- sqlparse/engine/__init__.py | 2 -- sqlparse/utils.py | 1 - 2 files changed, 3 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/engine/__init__.py b/sqlparse/engine/__init__.py index c30b6ca..3e2822b 100644 --- a/sqlparse/engine/__init__.py +++ b/sqlparse/engine/__init__.py @@ -61,8 +61,6 @@ class FilterStack(object): def _run1(stream): ret = [] for stmt in stream: - for i in stmt.flatten(): - print repr(i) for filter_ in self.stmtprocess: filter_.process(self, stmt) ret.append(stmt) diff --git a/sqlparse/utils.py b/sqlparse/utils.py index a349900..016a292 100644 --- a/sqlparse/utils.py +++ b/sqlparse/utils.py @@ -75,7 +75,6 @@ def memoize_generator(func): # Look if cached try: - print params cached = cache[params] # Not cached, exec and store it -- cgit v1.2.1 From 26986faad381f72078921d15f777024ff88fd15c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sat, 19 May 2012 23:52:53 +0200 Subject: Fixed bug on __getitem__ (infinite loop because pop() use it already) --- sqlparse/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/utils.py b/sqlparse/utils.py index 016a292..cdf27b1 100644 --- a/sqlparse/utils.py +++ b/sqlparse/utils.py @@ -20,8 +20,9 @@ if OrderedDict: self._maxsize = maxsize def __getitem__(self, key, *args, **kwargs): - # Remove the (key, value) pair from the cache, or raise KeyError - value = self.pop(key) + # Get the key and remove it from the cache, or raise KeyError + value = OrderedDict.__getitem__(self, key) + del self[key] # Insert the (key, value) pair on the front of the cache OrderedDict.__setitem__(self, key, value) -- cgit v1.2.1