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.
@inc.wrap()
def cached_fibonacci(num):  # Example fibonacci with caching
	if num < 2:
		return num
	return cached_fibonacci(num-1)+cached_fibonacci(num-2)
cached_fibonacci(40)

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}

Utils:

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

Encryption

The saved cache is encrypted by default, you can disable the encryption, or change the password (recommended)

Disabling encryption:

utils.save_full_cache("test.cache", inc, encrypt = False)  # Save
utils.load_full_cache("test.cache", encrypt = False)  # Load

Custom password (recommended):

utils.save_full_cache("test.enc", inc, password = "password")
utils.load_full_cache("test.enc", password = "password")

Filter

You can use your own function to filter the cache

Example:

import incached
inc = incached.INCached(cachesize=0)

def example_filter(args, kwargs):
    if args[0] % 2 != 0:
        return False
    return True

@inc.wrap(filter_func=example_filter)
def cached_fibonacci(num):  # Example fibonacci with caching
    if num < 2:
        return num
    return cached_fibonacci(num-1)+cached_fibonacci(num-2)

cached_fibonacci(100)
print(inc.cache_info())

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.3.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

incached-1.3-py3-none-any.whl (6.4 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