I used Flask-Caching to cache the response of a Flask view in Redis. Getting the cached data directly from Redis returns some bytes. How can I parse this in Python to examine the cached value?
b'!\x80\x03cflask.wrappers\nResponse\nq\x00)\x81q\x01}q\x02(X\x07\x00\x00\x00headersq\x03cwerkzeug.datastructures\nHeaders\nq\x04)\x81q\x05}q\x06X\x05\x00\x00\x00_listq\x07]q\x08X\x0c\x00\x00\x00Content-Typeq\tX\x10\x00\x00\x00application/jsonq\n\x86q\x0basbX\x0c\x00\x00\x00_status_codeq\x0cK\xc8X\x07\x00\x00\x00_statusq\rX\x06\x00\x00\x00200 OKq\x0eX\x12\x00\x00\x00direct_passthroughq\x0f\x89X\t\x00\x00\x00_on_closeq\x10]q\x11X\x08\x00\x00\x00responseq\x12Xn\x00\x00\x00[\n {\n "desc": "pronoun object", \n "tag": "CLO"\n }, \n {\n "desc": "pronoun", \n "tag": "CLS"\n }\n]q\x13X\x01\x00\x00\x00\nq\x14\x86q\x15ub.'
Flask-Cache uses the
RedisCache
backend provided by Werkzeug, which serializes values usingpickle.dumps
. It also prepends a!
for help when deserializing. You should typically not mess with these values directly, and let Flask-Caching handle it.You can use
pickletools.dis
to safely examine the representation, thenpickle.loads
to deserialize it. Security note:pickle.loads
can execute arbitrary code, so make sure you understand untrusted data first usingpickletools.dis
.