Caching engine for python
Project description
incached
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file incached-1.3.tar.gz
.
File metadata
- Download URL: incached-1.3.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0b7ef0c2f21e339ab8ab27ffe2d5ea3172c01e19415ebbed46aa8aec96a29ea |
|
MD5 | 14b12379d848b8f6eb1c5795bfbf973e |
|
BLAKE2b-256 | ce079b211265467112cd1570fea84559debdd4e9c07e77d2f60797505dfdf722 |
File details
Details for the file incached-1.3-py3-none-any.whl
.
File metadata
- Download URL: incached-1.3-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4517a81eac72b825532be84886df189fe3810840c02c50048001b063eb43a42f |
|
MD5 | 61f5f705cf8979f699fec716eb43dbd7 |
|
BLAKE2b-256 | 06cec8532fdaf9cd8df6e6a5fd4bb9ee65eb05eafd8a776834e51be6897d2cda |