Serializers#
Some alternative serializers are included, mainly intended for use with FileCache
.
Note
Some of these serializers require additional dependencies, listed in the sections below.
Specifying a Serializer#
Similar to Backends, you can specify which serializer to use with the serializer
parameter
for either CachedSession
or install_cache()
.
Built-in Serializers#
JSON Serializer#
Storing responses as JSON gives you the benefit of making them human-readable and editable, in exchange for a minor reduction in read and write speeds.
Usage:
>>> session = CachedSession('my_cache', serializer='json')
This will use ultrajson if installed, otherwise the stdlib
json
module will be used. You can install the optional dependencies for this serializer with:
pip install requests-cache[json]
YAML Serializer#
YAML is another option if you need a human-readable/editable format, with the same tradeoffs as JSON.
Usage:
>>> session = CachedSession('my_cache', serializer='yaml')
You can install the extra dependencies for this serializer with:
pip install requests-cache[yaml]
BSON Serializer#
BSON is a serialization format originally created for
MongoDB, but it can also be used independently. Compared to JSON, it has better performance
(although still not as fast as pickle
), and adds support for additional data types. It is not
human-readable, but some tools support reading and editing it directly
(for example, bson-converter for Atom).
Usage:
>>> session = CachedSession('my_cache', serializer='bson')
You can install the extra dependencies for this serializer with:
pip install requests-cache[mongo]
Or if you would like to use the standalone BSON codec for a different backend, without installing MongoDB dependencies:
pip install requests-cache[bson]
Response Content Format#
By default, any JSON or text response body will be decoded, so the response is fully
human-readable/editable. Other content types will be saved as binary data. To save all content as binary, set decode_content=False
:
>>> backend = FileCache(decode_content=False)
>>> session = CachedSession('http_cache', backend=backend)
Serializer Security#
See Security for recommended setup steps for more secure cache serialization, particularly
when using pickle
.
Custom Serializers#
If the built-in serializers don’t suit your needs, you can create your own. For example, if
you had a imaginary custom_pickle
module that provides dumps
and loads
functions:
>>> import custom_pickle
>>> from requests_cache import CachedSession
>>> session = CachedSession(serializer=custom_pickle)
Serializer Pipelines#
More complex serialization can be done with SerializerPipeline
. Use cases include
text-based serialization, compression, encryption, and any other intermediate steps you might want
to add.
Any combination of these can be composed with a SerializerPipeline
, which starts with a
CachedResponse
and ends with a str
or bytes
object. Each stage
of the pipeline can be any object or module with dumps
and loads
functions. If the object has
similar methods with different names (e.g. compress
/ decompress
), those can be aliased using
Stage
.
For example, a compressed pickle serializer can be built as:
Text-based Serializers#
If you’re using a text-based serialization format like JSON or YAML, some extra steps are needed to
encode binary data and non-builtin types. The cattrs library can do
the majority of the work here, and some pre-configured converters are included for several common
formats in the preconf
module.
For example, a compressed JSON pipeline could be built as follows:
Note
If you want to use a different format that isn’t included in preconf
, you can use
CattrStage
as a starting point.
Additional Serialization Steps#
Some other tools that could be used as a stage in a SerializerPipeline
include:
Class |
loads |
dumps |
---|---|---|
encode |
decode |
|
compress |
decompress |
|
compress |
decompress |
|
compress |
decompress |
|
compress |
decompress |
|
dumps |
loads |
|
sign |
unsign |
|
loads |
dumps |
|
encrypt |
decrypt |