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 three cache modes, memory cache mode, file cache mode, redis cache mode.
You can control the time of cache failure by using the cache parameters.
memory modes
from mecache import Memory
memory = Memory("CACHE_PATH")
# Cache failure after 60 seconds
@memory.cache(60)
def do(x, y):
import time
time.sleep(2)
return x+y
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
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
@memory.cache(60*60, keyf=key_by_user)
def home(request):
return render(request, 'home.html')
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
mecache-1.1.0.tar.gz
(4.5 kB
view hashes)
Built Distribution
Close
Hashes for mecache-1.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae456b1d130b62119e90f1591d80002e6723b08be222c2fe44a24a71f3788dd2 |
|
MD5 | 117bc90cb08daddfa65d75f129e5fdb3 |
|
BLAKE2b-256 | 06ea11901db76378e87b4214766684f2e9b63fb895764c201379f51a1a8036e0 |