Add salt.utils.memoize() to cache return output

I looked at several ways to do this and played with it a bit. This
was the shortest and most concise. It seems like there might be some
other places to do it, so I added it as a utility function.
This commit is contained in:
Jeff Schroeder 2012-11-03 22:18:57 -07:00
parent 4c26f60049
commit 025dcdaa6a

View File

@ -712,3 +712,14 @@ def str_to_num(text):
except ValueError:
return text
def memoize(func):
'''
Memoize aka cache the return output of a function
given a specific set of arguments
'''
cache = {}
def _m(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return _m