Skip to main content

Caching engine for python

Project description

incached

pipeline status Ultimate cache engine for Python3

Installation

Use the package manager pip to install incached.

pip install incached

Usage

import incached

inc = incached.INCached(cachesize=0)  # Set cachesize to infinite
def fibonacci(num):  # Example fibonacci without caching, try to run it
	if num < 2:
		return num
	return fibonacci(num-1)+fibonacci(num-2)

fibonacci(40)  # After 35 iterations, the performance will slow down considerably.
def cached_fibonacci(num):  # Example fibonacci with caching
	if num < 2:
		return num
	return inc.cache(cached_fibonacci, (num-1,))+inc.cache(cached_fibonacci, (num-1,))  # Explanation below
inc.cache(cached_fibonacci, (40,))  # Arguments is the function without calling, and the tuple with the arguments.

Try changing 40 to 400, the calculations are almost instantaneous compared to the non-cached function.

>>> print(inc.cache_info())  # Prints cache info
{'hits': 399, 'misses': 400, 'cachesize': 400}
>>> inc.save_cache("test.cache", save_stats=True)  # Save cache to file
>>> inc.clear_cache()  # Clear the cache
>>> inc.clear_stats()  # Clear hits and misses
>>> inc.load_cache("test.cache", load_stats=True)  # Load cache from file

Utils:

>>> from incached import utils
>>> utils.save_full_cache("test.full", inc)  # Fully save cache to file
>>> x = utils.load_full_cache("test.full")  # Load full cache from file
>>> print(inc.cache_info())  # Prints cache info
{'hits': 399, 'misses': 400, 'cachesize': 400}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

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

incached-1.1.2.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

incached-1.1.2-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

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