logo minicache

Python memory caching utilities for Python 2 and 3 versions, also PyPy.

by Eugene Duboviy

Build Status Codacy Badge PyPI Code Health PRs & Issues Welcome Awesome

Why?

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).

Current features

Installation

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

Supported python versions

PyPI

Examples

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

License

MIT licensed library. See LICENSE for details.

Contributing

If you have suggestions for improving the minicache, please open an issue or pull request on GitHub.

Badges

forthebadge forthebadge forthebadge forthebadge

forthebadge forthebadge forthebadge forthebadge

forthebadge