How to compress a string in Python to save it in Redis?

What library or method can I use in Python so that I can take a string and compress it for storage inside Redis? The goal is to reduce the size of some lines ( cPickled objects) when they pass through the wire, at the expense of some processing power.

I believe that the resulting object should have a type strbefore storage. I am using the library redis-py.

Many thanks!

+5
source share
2 answers

I recommend you zlib :

import zlib

compressedString = zlib.compress(originalString, 9)  # Compress at level 9

decompressedString = zlib.decompress(compressedString)
+5
source

Choose one . However, bz2 is cool.

+3
source

All Articles