Skip to main content

mecache

An easy-to-use cache module

How to use

Run pip install mecache or pipenv install mecache to install.

Built-in two cache modes, file cache mode, redis cache mode.

You can control the time of cache failure by using the cache parameters.

redis modes

from mecache import Redis

# Refer to https://redis-py.readthedocs.io/en/latest/ for all parameters
redis = Redis(host="127.0.0.1", port='6379', db=0, password="password")

# Cache failure after 60 seconds
@redis.cache(60)
def do(x, y):
    import time
    time.sleep(2)
    return x+y

asynchronous redis modes

from mecache import AioRedis

# Refer to https://aioredis.readthedocs.io/en/latest/api_reference.html#aioredis.create_redis_pool for all parameters
file = await AioRedis('redis://localhost')

# Cache failure after 60 seconds
@file.cache(60)
async def do(x, y):
    return await do.something

file modes

from mecache import File

# CACHE_PATH like '/var/tmp/test-cache'
file = File("CACHE_PATH")

# Cache failure after 60 seconds
@file.cache(60)
def do(x, y):
    import time
    time.sleep(2)
    return x+y

asynchronous file modes

from mecache import AioFile

# CACHE_PATH like '/var/tmp/test-cache'
file = AioFile("CACHE_PATH")

# Cache failure after 60 seconds
@file.cache(60)
async def do(x, y):
    return await do.something

Advanced usage

If the parameters of a function are difficult to serialize using pickle, you can specify the rules that generate key by customizing keyf. The return value of the function keyf must be a string.

# a example in django view function

def key_by_user(request):
    return request.user.username

@file.cache(60*60, keyf=key_by_user)
def home(request):
    return render(request, 'home.html')

You can also overwrite keyf for all cache. This is a example:

from mecache import File

class CustomFile(File):
    @staticmethod
    def keyf(*args, **kwargs):
        string = do.something
        return string

file = CustomFile("CACHE_PATH")

Custom made

If you need a custom cache, you can use BaseCache or AioBaseCache to create your cache class. Like this

from mecache import BaseCache

class CustomCache(BaseCache):

    def get_cache(self, func, key, max_time):
        qual = func.__qualname__
        return get(qual+":"+key)

    def set_cache(self, result, func, key, max_time):
        qual = func.__qualname__
        set(qual+":"+key, result, ex=time.time()+max_time)

Metadata

Release files for mecache 1.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mecache 1.4.2
File Size Uploaded
mecache-1.4.2.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mecache 1.4.2
File Interpreter ABI Platform
mecache-1.4.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 11.9 kB

Release files / mecache-1.4.2.tar.gz

Download URL mecache-1.4.2.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ddf5acb9139ef46fe89cd41bd7f418813ebb120af1348f600e0ae33e5135d243
BLAKE2b-256 checksum
How to use checksums
8d46ba8b17dbf1a2824d609c7e68188147075793c5a1b454cff6bab1dc93123e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.35.0 CPython/3.7.4

Release files / mecache-1.4.2-py2.py3-none-any.whl

Download URL mecache-1.4.2-py2.py3-none-any.whl
Size 6.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b110d07e9fcf40bc1b9a23fb298c7c4c92f04dff12100d63d2c23b05e24c3d34
BLAKE2b-256 checksum
How to use checksums
f5acf5bf64e4b502502531ff000368570781fd2fcc09b73c9280d92a82a49378
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.35.0 CPython/3.7.4

Release history Release notifications | RSS feed

This release

1.4.2 This release

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page