Snap-in memory caching for Python, no persistence, just pure function result caching.
Project description
SnapCache
Snap-in memory caching for Python, no persistence, just pure function result caching.
Motivation
Python’s built-in functools.lru_cache
offers an easy way to cache function results, but it has limitations. For example, attempting to cache complex objects like NumPy
arrays results in a TypeError: unhashable type: 'numpy.ndarray'
.
SnapCache addresses this issue by offering a similar simple decorator interface while supporting caching for any data structure ✨.
Installation
To install SnapCache, use pip
:
pip install snapcache
Usage
Function-level Caching
You can cache function results using the WithCache
decorator. Just apply it to any function you want to memoize.
from snapcache import WithCache
@WithCache(maxsize=3)
def add(a, b):
return a + b
# Call the function multiple times
print(add(1, 2)) # First call, result is computed and cached
print(add(1, 2)) # Second call, result is retrieved from cache
Method-level Caching
You can also apply the decorator to class methods:
class Foo:
@WithCache(maxsize=3)
def bar(self, a, b):
return a - b
foo = Foo()
print(foo.bar(10, 5)) # First call, result is computed and cached
print(foo.bar(10, 5)) # Second call, result is retrieved from cache
License
This project is licensed under the MIT License - see the LICENSE file for details.
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 snapcache-1.0.0.tar.gz
.
File metadata
- Download URL: snapcache-1.0.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef48ae47c2c6aa90be8df772d47dc5dd9859892f8873917f09d6223bc6c8edc7 |
|
MD5 | 498eae6673501bc36902a5169cb108f2 |
|
BLAKE2b-256 | 701e77a4a03c5befb8023b98953ff075b7f2d68234b617c4583d1f41901060bc |
File details
Details for the file snapcache-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: snapcache-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 524aea906b696879a27ccdeea18e7f518cad325ea8a0acfdbb69efd9fce2ec27 |
|
MD5 | a43eacfcd81c567799a8f5a012450c84 |
|
BLAKE2b-256 | 451901ab712570e6cb460e4e349865286c8549758e9908502fadcba30165b588 |