pypi Flask-Caching 2.5.0

8 hours ago

Note: This release will invalidate your caches because we changed the default hash_method from hashlib.md5 to hashlib.sha256. This can be changed back via CACHE_HASH_METHOD if you wish to still use the old hash_method!

Added / Improved

  • Specifying timeouts now also works with datetime.timedelta. #266
  • Add config option CACHE_SERIALIZER to set the cachelib serializer the backend uses. #209
  • cachelib backends can now be configured directly via CACHE_TYPE="cachelib.ValkeyCache. #668
  • Send Signals for cache hits and misses. #237 and #667
  • Add Cache.delete_cached() and extend make_cache_key() with path and query_args arguments to make deleting views decorated with cached(query_string=True) possible. #243
  • Add CACHE_HASH_METHOD to set the hash method used for the cache keys of @cached and @memoize.
  • Add an is_stale option to @cached and @memoize. It is called on a cache hit with the cached value. Unlike forced_update it uses the value itself to check whether the cache is stale. Can be used in combination with forced_update #392
  • @cached and @memoize now preserve the wrapped function's signature and expose uncached, cache_timeout, make_cache_key delete_memoized to type checkers.
  • Add CACHE_FILE_HASH_METHOD config option to FileSystemCache to allow using a different hash function for cache keys. #660
  • Add pool_size and pool_blocking to make MemcachedCache backends thread-safe. #663
  • Document that CACHE_REDIS_HOST accepts an already created redis.Redis client instead of a host name, which allows sharing a connection pool with other extensions and the application. #629
  • Clarify docs about decorator order regarding @staticmethod and @classmethod when memoizing. #440
  • Modify SimpleCache docs as it's now thread-safe using an RLock for all operations. #663

Changed

  • Drop support for Python 3.10 and require cachelib 0.17.0+
  • Remove the deprecated lowercase CACHE_TYPE names (null, simple, filesystem, redis, redissentinel, rediscluster, uwsgi, memcached, gaememcached, saslmemcached and spreadsaslmemcached)
  • Use hashlib.sha256 instead of hashlib.md5 for hashing the cache keys. This changes the generated keys, so entries cached by an earlier version become obsolete. If you wish to still use hashlib.md5 set the config CACHE_HASH_METHOD = hashlib.md5. #563
  • @cached and @memoize now go through the public Cache proxy methods (get, set, has, delete, delete_many, get_many and set_many) instead of calling the backend directly. #417
  • CACHE_IGNORE_ERRORS is now used by every backend instead of only SimpleCache and FileSystemCache.
  • The delete_many method is now used from cachelib. A key that does not exist counts as deleted, and with CACHE_IGNORE_ERRORS set to False a RuntimeError is raised if the key could not be deleted.
  • Call @memoize forced_update callbacks once per decorated function call instead of once while making the key and again before cache lookup. #387
  • @cached now caches an HTTPException raised by the view (i.e. through abort()). This exception will now be re-raises on a cache hit. Use response_filter, to keep the exception out of the cache. #444
  • The {% cache %} Jinja tag no longer propagates backend errors. If the cache is unavailable, the block is rendered normally instead of raising, matching the existing behavior of @memoize. #564 #565

Fixed

  • Include key_prefix when building @cached(query_string=True) cache keys. #302
  • Fix a @memoize cache-key collision when a parameter has a falsy default (e.g. 0, "", False): calling with the default was keyed the same as passing None, returning the wrong cached result. #656
  • Fix @cached(response_hit_indication=True) appending a new after_request function to the app on every request.
  • Fix a view returning an iterator of strings, for example when using flask.stream_template it previously returned a list of JSON strings instead of the rendered template. #511
  • Fix __caching_id__ never being usable. @memoize silently ignored that and ran the function uncached.
  • delete_memoized now takes the instance from a bound method, so cache.delete_memoized(obj.method, 1) deletes the cache for obj.method(1) instead of silently deleting nothing. The current behaviour by passing the instance keeps working as well. #554
  • Fix @memoize invalidating a function's entire cache by not updating the memoize version key's timeout. It is now refreshed each time an entry is written, with that entry's own timeout. #531

New Contributors

Full Changelog: v2.4.1...v2.5.0

Don't miss a new Flask-Caching release

NewReleases is sending notifications on new releases.