django-cache-memoize is a Django utility that provides a memoization decorator, `cache_memoize`, which leverages the Django cache framework. It's designed to cache function call results, supports invalidation, and works with complex arguments and keyword arguments. The current version is 0.2.1, and it maintains an active release cadence.
pip install django-cache-memoizeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the `cache_memoize` decorator on an expensive function within a minimal Django setup. It configures a local memory cache, applies the decorator with a timeout, and shows how subsequent calls with the same arguments retrieve cached results. It also illustrates how to manually invalidate a specific cached entry.
Ensure that the function returns only pickleable objects. For unpickleable items, consider caching only their serializable attributes or using Django's low-level cache API directly with custom serialization.
Add a `CACHES` dictionary to your `settings.py` file, defining at least a 'default' cache backend (e.g., `django.core.cache.backends.locmem.LocMemCache`).
Store or reconstruct the exact arguments used for caching if you need to invalidate specific entries. If broad invalidation is required, consider alternative strategies or custom cache key generation and management.
Pass immutable objects (e.g., tuples, integers, strings) as arguments where possible. If mutable objects must be used, ensure they have a `__str__` method that produces a unique and consistent string representation for the object's identity relevant to caching.