utility function for caching form data
This commit is contained in:
parent
41ba98e3b6
commit
fea85cb07b
18
atst/utils/form_cache.py
Normal file
18
atst/utils/form_cache.py
Normal 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)
|
15
tests/utils/test_form_cache.py
Normal file
15
tests/utils/test_form_cache.py
Normal file
@ -0,0 +1,15 @@
|
||||
from werkzeug.datastructures import ImmutableDict
|
||||
|
||||
from atst.utils.form_cache import DEFAULT_CACHE_NAME, cache_form_data, retrieve_form_data
|
||||
|
||||
def test_cache_form_data(app):
|
||||
data = ImmutableDict({"kessel_run": "12 parsecs"})
|
||||
key = cache_form_data(app.redis, data)
|
||||
assert app.redis.get("{}:{}".format(DEFAULT_CACHE_NAME, key))
|
||||
|
||||
|
||||
def test_retrieve_form_data(app):
|
||||
data = ImmutableDict({"class": "corellian"})
|
||||
key = cache_form_data(app.redis, data)
|
||||
retrieved = retrieve_form_data(app.redis, key)
|
||||
assert retrieved == data
|
Loading…
x
Reference in New Issue
Block a user