a simple cache lib support memory、file、redis
Project description
Mo-Cache
a simple cache lib support memory、file、redis
Github: https://github.com/mouday/mo-cache
install
pip install mo-cache
demo
快速开始
from mo_cache import MemoryCache
cache = MemoryCache()
# 此时的key默认是函数名 foo
@cache
def foo(a, b):
return a + b
if __name__ == '__main__':
foo()
更多示例
1、使用装饰器
# 主动设置key, 过期时间单位:秒s
@cache(key='custom_key', expire=30)
def foo(a, b):
return a + b
2、主动设置和获取
cache.set(key='custom_key', value='value', expire=30)
value = cache.get(key='custom_key')
MemoryCache, FileCache, RedisCache实现了共同的接口(set/get/call),
只是构造参数稍有不同
from mo_cache import MemoryCache, FileCache, RedisCache
memory_cache = MemoryCache()
file_cache = FileCache(cache_dir='cache')
redis_cache = RedisCache(redis_url='redis://localhost:6379/0')
继承体系
class CacheAbstract(object):
"""统一的接口"""
def set(self, key, value, expire=-1):
pass
def get(self, key):
pass
class CacheDecorator(CacheAbstract):
"""cache 装饰器"""
def __call__(self, key=None, expire=-1):
pass
class MemoryCache(CacheDecorator):
"""内存缓存"""
class FileCache(CacheDecorator):
"""文件缓存"""
class RedisCache(CacheDecorator):
"""Redis 缓存"""
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
mo-cache-0.0.8.tar.gz
(4.7 kB
view details)
Built Distribution
File details
Details for the file mo-cache-0.0.8.tar.gz
.
File metadata
- Download URL: mo-cache-0.0.8.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fd1561dca04a8f780b50ca55d1d4ea66aa989405872ac7a638b50f98e3e8f87 |
|
MD5 | e85ab233cd162dc8d6e8f7e421c88ac1 |
|
BLAKE2b-256 | b720f9a90499a89827cf892f47e175d57494ed83987ab3c5d242b473d85eeffc |
File details
Details for the file mo_cache-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: mo_cache-0.0.8-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51dececac4db8a51daa38abf9cde951822ee2796ed5d35c422a6f0ce935cd06d |
|
MD5 | 03da5e36284a3a9b4ca9c44b8c78b4b6 |
|
BLAKE2b-256 | c3f126689a304c84b631464790d5a87a0da69e4eecfc3c657fb8e5b1ba66aa9a |