- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to compress Python objects before saving to cache?
We need sometimes to compress Python objects (list, dictionary, string, etc) before saving them to cache and decompress after reading from cache.
Firstly we need to be sure we need to compress the objects. We should check if the data structures/objects are too big just to fit uncompressed in the cache. There is going to be an overhead for compression/decompression, that we have to tradeoff with the gains made by caching in the first place.
If we really need compression, then we probably want to use zlib.
If we are going to use zlib, we might want to experiment with the different compression levels available in the compress method, to balance CPU time vs compression levels:
Compresses the data in string, returning a string contained compressed data. level is an integer from 1 to 9 controlling the level of compression; 1 is fastest and produces the least compression, 9 is slowest and produces the most. The default value is 6. Raises the error exception if any error occurs.
- Related Articles
- How to Compress files with ZIPFILE module in Python.
- How to access Python objects within objects in Python?
- How we can compress large Python files?
- How to find all objects created before specified Date in MongoDB?
- How to compress and un-compress the data of a file in Java?
- How to compress a file in Java?
- How to un-compress a file in Java?
- Best Way to compress mysqldump?
- Program to implement Least Frequently Used Cache in Python
- How to create class objects in Python?
- LFU Cache in Python
- How to display an image/screenshot in a Python Tkinter window without saving it?
- How to cache query results in Oracle?
- Generating a movie from Python without saving individual frames to files