Skip to main content

Like @functools.cache / @functools.lru_cache but with non-hashable objects

Project description

Like @functools.cache / @functools.lru_cache but with non-hashable objects

Tested against Windows 10 / Python 3.11 / Anaconda

pip install allescacher

Cache Everything Decorator

This module provides a decorator cache_everything that can be used to cache the results of function calls. It uses a dictionary to store cached results and allows customization of cache size and eviction strategy. Unlike some caching mechanisms that rely on hashable arguments, the "cache_everything" decorator accepts a wide range of argument types, including non-hashable objects, complex data structures, and even functions as arguments. This means you can cache the results of functions that operate on mutable or unhashable input data, making it suitable for a broader set of use cases. For example, if your function operates on lists, dictionaries, or custom objects, you can still apply the decorator without the need to convert these inputs into hashable forms. This flexibility simplifies the caching process and allows you to cache results efficiently for functions that work with diverse and potentially non-hashable data.

Usage:
	To cache the results of a function, decorate it with `@cache_everything`.

Example:
	import numpy as np
	import random
	from allescacher import cache_everything, cache_dict_module

	cache_dict_module.maxsize = None
	cache_dict_module.del_min_used = True

	@cache_everything
	def getnparray(l):
		return np.array(l) / 10 * 3

	done = []
	for _ in range(1000):
		done.append(
			getnparray([random.randint(0, 10) for x in range(random.randint(1, 5))])
		)

	# Clear the cache for the 'getnparray' function
	cache_dict_module.cache_dict[getnparray.__qualname__].clear()

Module Variables:
	- `cache_dict_module`: A module-level variable used to store the cache dictionary.
	- `cache_dict_module.cache_dict`: The cache dictionary where cached results are stored.
	- `cache_dict_module.maxsize`: The maximum size of the cache (None for unlimited).
	- `cache_dict_module.del_min_used`: A flag to determine whether to evict the least-used cache entry.

Decorator:
	- `@cache_everything`: Decorates a function to enable caching of its results.

Functions:
	- `cache_everything(f_py=None)`: The decorator function that can be used to cache function results.

This module provides a flexible caching mechanism that can be applied to functions to improve performance by
retrieving cached results for known input arguments, reducing the need for redundant computations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

allescacher-0.10.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

allescacher-0.10-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file allescacher-0.10.tar.gz.

File metadata

  • Download URL: allescacher-0.10.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for allescacher-0.10.tar.gz
Algorithm Hash digest
SHA256 0780b7996b72c4c7613c61f426aeb0daa071e4e2d0f435b7e077cd7d0159013b
MD5 20fb2093b143b11887a668d46a8ddc37
BLAKE2b-256 44f598f691969d5c795802e444c83891497685c10ab9ba94d4c00310f41169bb

See more details on using hashes here.

File details

Details for the file allescacher-0.10-py3-none-any.whl.

File metadata

  • Download URL: allescacher-0.10-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for allescacher-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 29486f4bee376868f6152541f2143455bd4292d5be24fc7a5a76b839d48e4ffa
MD5 272e0ae291dcedbaa19f24a5329886cc
BLAKE2b-256 46f9ebaa0f6055355f851dbf712d896b9ce6affaecec43fbe0fd1f0ee4de72f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page