Python memory caching utilities for Python 2 and 3 versions, also PyPy.
A major problem of funcy.memoize you couldn't test with it because there was no (obvious) way to turn it off. This project was created to suit the "memoization" needs, with a hook to turn it off (for testing or other purposes).
- Simple set and get workflow
- Decorator for "memoization" class methods and functions
- Enabling and disabling functionality (including a context manager)
- No additional packages required to be installed (using only standard python lib)
Install from PyPI:
pip install minicache
Or using alternative command:
pip install https://github.com/duboviy/minicache/archive/master.zip
Or from source use:
python setup.py install
- 2.7
- 3.3
- 3.4
- 3.5
- PyPy
Basic usage
>>> from minicache import cache
>>> cache.has('key')
False
>>> cache.get('key', default='default')
'default'
>>> cache.update('key', 'value')
>>> cache.get('key')
'value'
>>> cache.disable()
>>> cache.get('key')
Decorator and context manager
from minicache import cache
import time
@cache.this
def somefunc():
time.sleep(5)
return "this will be cached, and you won't have to wait a second time!"
def test_somefunc():
somefunc()
somefunc()
with cache.temporarily_disabled():
# now we'll have to wait again
somefunc()
Decorator for "memoization" class methods:
class Foo(Cacheable):
def __init__(self):
super(Foo, self).__init__()
self._bar = 5
@property
@Cacheable.cached
def m1(self):
print('actual call property...', self._bar)
return self._bar
@Cacheable.cached
def m2(self, a, b, k=4, m=10):
s = a + b + k + m
print('actual call method...', a, b, k, m, s)
return s
@Cacheable.cached
def m3(self, a=3, b=2):
s = a + b
print('actual call method (kwargs only)', a, b, s)
return s
... and many other features
MIT licensed library. See LICENSE for details.
If you have suggestions for improving the minicache, please open an issue or pull request on GitHub.