Cachier is a Python library that provides persistent, stale-free memoization decorators for Python functions. It supports various storage backends including local filesystem (pickle), in-memory, MongoDB, Redis, SQL, and S3, offering configurable cache expiration and automatic invalidation. The library is actively maintained with frequent releases, currently at version 4.2.0, and supports both synchronous and asynchronous functions.
pip install cachierVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the `cachier` decorator to add a persistent, time-based cache to a Python function. The first call computes the result, and subsequent calls within the `stale_after` period return the cached value instantly. It also shows how to manually clear a function's cache.
Be aware of this change when upgrading. Existing cache entries might need to be cleared or regenerated if function names were not implicitly part of your keying strategy.
If using global enable/disable, verify its behavior after upgrading. If relying on cache file locations, check `~/.cache/cachier` (or `$XDG_CACHE_HOME/cachier`) and potentially migrate existing cache files.
For instance methods, use `@cachier(allow_non_static_methods=True)` or ensure the method is a `@staticmethod` or `@classmethod` if shared caching is not desired.
Ensure all arguments to cached functions are hashable. For functions with unhashable arguments, consider providing a custom `hash_func` to the decorator to generate a hashable key from the unhashable inputs.
For optimal and correct async caching, ensure you are using backends and client configurations that natively support async operations, or understand the delegation behavior for your chosen backend.
Pass `allow_none=True` to the decorator: `@cachier(allow_none=True)`.
To explicitly allow caching of instance methods, pass `allow_non_static_methods=True` to the `@cachier` decorator. Ensure this is the desired behavior, as it means the cache will be shared across all instances of the object.
```python
from cachier import cachier
class Foo:
@cachier(allow_non_static_methods=True)
def my_method(self, arg1):
return arg1 * 2
```To instruct Cachier to cache `None` values, set `allow_none=True` in the `@cachier` decorator.
```python
from cachier import cachier
@cachier(allow_none=True)
def function_returning_none(arg):
if arg > 0:
return arg
return None
```Verify that the Redis server is running and accessible from the application's environment. Check the Redis configuration for the correct host and port, ensure no firewalls are blocking the connection (e.g., port 6379 for Redis), and confirm `cachier` is configured with the correct client or connection string for Redis.
```python
# Example for Redis in cachier, assuming redis_client is correctly initialized
import redis
from cachier import cachier
# Ensure your Redis server is running, e.g., 'redis-server'
# Example of setting up cachier with a Redis client
REDIS_CLIENT = redis.StrictRedis(host='localhost', port=6379, db=0)
@cachier(backend='redis', redis_client=REDIS_CLIENT)
def my_cached_function_redis():
return "Data from Redis"
```Increase the system's `inotify` watch limit. This is a system-level configuration, not a `cachier` code fix. Alternatively, consider using a different `cachier` backend like MongoDB or Redis if you anticipate a very large number of cached items, as they don't rely on `inotify` for cache file monitoring. ```bash # To increase the inotify watch limit (e.g., to 524288) # Add this line to /etc/sysctl.conf echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf # Apply the change sudo sysctl -p ```