Merge pull request #1315 from anandpdoshi/local-cache

frappe.local_cache(namespace, key, generator) for in request caching
This commit is contained in:
Anand Doshi 2015-09-25 17:12:32 +05:30
commit 2f8899eaf9

View file

@ -1033,3 +1033,19 @@ def publish_realtime(*args, **kwargs):
import frappe.async
return frappe.async.publish_realtime(*args, **kwargs)
def local_cache(namespace, key, generator):
"""A key value store for caching within a request
:param namespace: frappe.local.cache[namespace]
:param key: frappe.local.cache[namespace][key] used to retrieve value
:param generator: method to generate a value if not found in store
"""
if namespace not in local.cache:
local.cache[namespace] = {}
if key not in local.cache[namespace]:
local.cache[namespace][key] = generator()
return local.cache[namespace][key]