API Reference

mongo_memoize.memoize(db_name='mongo_memoize', host='localhost', port=27017, collection_name=None, prefix='memoize', capped=False, capped_size=100000000, capped_max=None, connection_options={}, key_generator=None, serializer=None)

A decorator that caches results of the function in MongoDB.


>>> from mongo_memoize import memoize
>>> @memoize()
... def some_function():
...     pass
  • db_name (str) – MongoDB database name.
  • host (str) – MongoDB host name.
  • port (int) – MongoDB port.
  • collection_name (str) – MongoDB collection name. If not specified, the collection name is generated automatically using the prefix, the module name, and the function name.
  • prefix (str) – Prefix of the MongoDB collection name. This argument is only valid when the collection_name argument is not specified.
  • capped (bool) – Whether to use the capped collection.
  • capped_size (int) – The maximum size of the capped collection in bytes.
  • capped_max (int) – The maximum number of items in the capped collection.
  • connection_options (dict) – Additional parameters for establishing MongoDB connection.
  • key_generator – Key generator instance. PickleMD5KeyGenerator is used by default.
  • serializer – Serializer instance. PickleSerializer is used by default.
class mongo_memoize.NoopSerializer

Serializer that does nothing.


It is required that the result of the function can be stored directly in MongoDB when you use this serializer.

class mongo_memoize.PickleSerializer(protocol=-1)

Serializer using Pickle.

Parameters:protocol (int) – Pickle protocol version.
class mongo_memoize.PickleMD5KeyGenerator(protocol=-1)

Cache key generator using Pickle and MD5.

Parameters:protocol (int) – Pickle protocol version.