Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What library or method can I use in Python so I can take a string and compress it to be stored inside of Redis? The objective is to reduce the size of some strings (cPickled objects) as they go through the wire, at the cost of some computational power.

I believe that the resulting object has to be of type str before storage. I am using the redis-py library.

Thanks very much!

like image 958
Juan Carlos Coto Avatar asked Apr 16 '13 17:04

Juan Carlos Coto


1 Answers

I recommend you zlib:

import zlib

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

decompressedString = zlib.decompress(compressedString)
like image 50
pmoleri Avatar answered Oct 15 '22 13:10

pmoleri