From 8b3e88e60d8773ef2432bdc511ed7eaf1dc09a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Thu, 17 May 2012 22:34:54 +0200 Subject: Added memoize_generator and use it on IncludeStatement --- sqlparse/utils.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 sqlparse/utils.py (limited to 'sqlparse/utils.py') diff --git a/sqlparse/utils.py b/sqlparse/utils.py new file mode 100644 index 0000000..0dbb09f --- /dev/null +++ b/sqlparse/utils.py @@ -0,0 +1,41 @@ +''' +Created on 17/05/2012 + +@author: piranna +''' + + +def memoize_generator(func): + """ + Memoize decorator for generators + + Store `func` results in a cache according to their arguments as 'memoize' + does but instead this works on decorators instead of regular functions. + Obviusly, this is only useful if the generator will always return the same + values for each specific parameters... + """ + cache = {} + + def wrapped_func(*args, **kwargs): + params = (args, kwargs) + + # Look if cached + try: + cached = cache[params] + + # Not cached, exec and store it + except KeyError: + cached = [] + + for item in func(*args, **kwargs): + cached.append(item) + yield item + + cache[params] = cached + + # Cached, yield its items + else: + for item in cached: + yield item + + return wrapped_func -- cgit v1.2.1 From 7e532bcd9af6f36280f497346d50b4cdd028cfbf 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 19:21:00 +0200 Subject: Added limit to cache --- sqlparse/utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'sqlparse/utils.py') diff --git a/sqlparse/utils.py b/sqlparse/utils.py index 0dbb09f..fd6651a 100644 --- a/sqlparse/utils.py +++ b/sqlparse/utils.py @@ -6,8 +6,7 @@ Created on 17/05/2012 def memoize_generator(func): - """ - Memoize decorator for generators + """Memoize decorator for generators Store `func` results in a cache according to their arguments as 'memoize' does but instead this works on decorators instead of regular functions. @@ -25,6 +24,12 @@ 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