Skip to main content

An easy-to-use cache module

Project description

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

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mecache-1.3.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mecache-1.3.0-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file mecache-1.3.0.tar.gz.

File metadata

  • Download URL: mecache-1.3.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6

File hashes

Hashes for mecache-1.3.0.tar.gz
Algorithm Hash digest
SHA256 6b4954c929cb6cb9e84e3ec3e83e3f4d06f32e07951510e99405c874e68c6f20
MD5 ffc585a2cf71175a7c407cc5def109db
BLAKE2b-256 b799cd46d7db66856d9aa3675fea6e8afb81b1154142059adc032f0d46189867

See more details on using hashes here.

File details

Details for the file mecache-1.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: mecache-1.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6

File hashes

Hashes for mecache-1.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1162b4812f1585a64e5c39d44217130903468a88d6c8f50eba56bc97911b8db6
MD5 472d9e6c1e76eb478e1b40a5a181b72d
BLAKE2b-256 ea2547d5af073bd069a8c8a323317a6d935a713041f814803987f3bc48a6a216

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page