utility function for caching form data

This commit is contained in:
dandds
2018-11-08 16:23:08 -05:00
committed by richard-dds
parent 41ba98e3b6
commit fea85cb07b
2 changed files with 33 additions and 0 deletions

18
atst/utils/form_cache.py Normal file
View File

@@ -0,0 +1,18 @@
import os
import hashlib
import pickle
DEFAULT_CACHE_NAME = "formcache"
def cache_form_data(redis, formdata, expiry_seconds=3600, key_prefix=DEFAULT_CACHE_NAME):
value = pickle.dumps(formdata)
key = hashlib.sha1(os.urandom(64)).hexdigest()
redis.setex(name="{}:{}".format(key_prefix, key), value=value, time=expiry_seconds)
return key
def retrieve_form_data(redis, formdata_key, key_prefix="formcache"):
data = redis.get("{}:{}".format(key_prefix, formdata_key))
return pickle.loads(data)