Registry / database / django-cache-memoize

django-cache-memoize

JSON →
library0.2.1pypypiunverified

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-memoize
INSTALL
IMPORT
SIG · DJANGO-CACHE-MEMOI
D
django-cache-memoize
databasepythonv0.2.1
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cache_memoize
from cache_memoize import cache_memoize
from cache_memoize import cache_memoize

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.

import random from django.conf import settings from django.core.cache import cache from django.http import HttpResponse from cache_memoize import cache_memoize # Minimal Django settings for cache configuration if not settings.configured: settings.configure( CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unique-snowflake', } } ) # Example of a function that would be expensive @cache_memoize(timeout=60 * 5) # Cache for 5 minutes def expensive_calculation(a, b): print(f"Calculating for {a}, {b}...") # This will only print on cache miss return a + b + random.randint(0, 100) # Example Django view def my_view(request): result1 = expensive_calculation(10, 20) result2 = expensive_calculation(10, 20) # This should be a cache hit result3 = expensive_calculation(1, 2) # Invalidate cache for specific arguments # expensive_calculation.invalidate(10, 20) # print("Invalidated (10, 20)") # result_after_invalidation = expensive_calculation(10, 20) return HttpResponse( f"Result 1 (10, 20): {result1}<br>" + f"Result 2 (10, 20) (cached): {result2}<br>" + f"Result 3 (1, 2): {result3}<br>" + f"Cache key for (10, 20): {expensive_calculation.get_memoize_key(10, 20)}" ) # To run this, you would integrate it into a Django project's urls.py # e.g., path('my_cached_view/', my_view), # and ensure Django's cache settings are configured (as above or in settings.py)
Debug
Known issues
gotchaThe decorated function's return value must be pickleable by Python's `pickle` module. If the return value is not pickleable (e.g., certain unpicklable object instances, database connections), caching will fail.
fix
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.
affects: All versions
gotchaTo effectively use `django-cache-memoize`, Django's caching framework must be properly configured in your project's `settings.py` (e.g., using `LocMemCache`, `RedisCache`, or `MemcachedCache`). If no cache backend is configured, the decorator will not store or retrieve values.
fix
Add a `CACHES` dictionary to your `settings.py` file, defining at least a 'default' cache backend (e.g., `django.core.cache.backends.locmem.LocMemCache`).
affects: All versions
gotchaCache invalidation using `.invalidate(*args, **kwargs)` requires providing the exact same arguments (positional and keyword) that were used when the function was originally called. There is no built-in mechanism to invalidate cache entries based on partial argument matches or patterns.
fix
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.
affects: All versions
gotchaUsing mutable objects (like lists or custom class instances without a stable `__str__` method) as arguments to a memoized function can lead to unexpected cache key generation or misses, as the default key generation relies on `str()` representation of arguments.
fix
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.
affects: All versions
Upgrade
Version history
0.2.1latest on PyPI · released Dec 18, 2024
Audit
Dependencies
DjangorequiredCore framework dependency for cache integration.
Agent activity
22 hits · last 30 days
node
22
Resources