0 Star 0 Fork 0

小为/wrapcache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

wrapcache

一个基于Python装饰器Decorators的方法缓存系统,用于缓存Python方法的输出值,可以支持复杂数据类型,可以缓存到Redis中、Python dict、LUR算法存储中。English README.md

Build Status codecov Status PyPi Status Python Versions PyPi Downloads

Homepage | Usage & Doc | Source Code | Issue & Bug

什么是 wrapcache?

wrapcache 是一个可以缓存方法输出的装饰器,即简单的缓存方法的输出值。

缓存数据的键值Key完全依赖于方法和传入方法的参数,这部分完全透明,使用起来非常方便。

同时还提供部分API方法来通过代码获取缓存、删除缓存,支持Python2.6 ~ Python3.5

看了下面的一个示例,你就明白如何使用了:

DEMO


import wrapcache

from time import sleep
import random

@wrapcache.wrapcache(timeout = 3)
def need_cache_function(input, t = 2, o = 3):
    sleep(2)
    return random.randint(1, 100)

if __name__ == "__main__":
	print(need_cache_function(1, t = 2, o = 3)) #会在2秒之后打印随机数(例如59)
	print(need_cache_function(1, t = 2, o = 3)) #会很快就数据缓存数据59,不需要等待2秒
	sleep(3) # cache timeout
	print(need_cache_function(1, t = 2, o = 3)) #会在2秒后打印另外一个随机数,一般不会等于59


配置项

@wrapcache.wrapcache(timeout = timeout, adapter = adapter)
  • timeout: 缓存会持续多长时间,单位为秒,如果为-1,表示不缓存。
  • adapter: 缓存到何处?RedisAdapterMemoryAdapter 可选. 默认 MemoryAdapter.

缓存存储器

当使用MemoryAdapterRedisAdapter适配的时候,需要在使用之前设置他们的数据存储器

MemoryAdapter存储器

对于MemoryAdapter来说,提供两种缓存器:

  1. 一种是基本的Python字典{},也是MemoryAdapter存储器默认的存储器。可以无限量存储数据,具有最高的效率。但是需要手动进行清理缓存,否则可能导致内存泄露,建议在Key值比较固定的方法是哪个使用,例如无参数的方法。
  2. 一种是本项目提供的LruCacheDB存储器(加入了LUR算法的Python字典)。LUR算法经过优化,所有操作的算法复杂度均为o(1),使用时需要指定size,为缓存的大小,默认为-1,不限定大小,建议按照个人项目需要进行设置。

例如:

from wrapcache.database import LruCacheDB
lruDB = MemoryAdapter.db = LruCacheDB(size = 100)
RedisAdapter.db = lruDB

RedisAdapter存储器

当使用RedisAdapter,必须设置其存储器,否则无法使用,RedisAdapter存储器极为redis连接实例,需要pip install redis。例如:

REDIS_POOL = redis.ConnectionPool(host = '10.246.13.1', port = 6379, password = 'redis_pwd', db = 1)
REDIS_INST = redis.Redis(connection_pool = REDIS_POOL, charset = 'utf8')
RedisAdapter.db = REDIS_INST

如何安装使用?

安装

三种方法安装 wrapcache:

1. 使用PIP工具

  • pip install wrapcache

2. 下载安装

3. 手动安装使用

  • 将项目中的wrapcache 复杂到当前目录,或者 Python的site-packages目录, 然后 import wrapcache 即可使用。

使用方法

装饰器


import wrapcache
@wrapcache.wrapcache(timeout = 3)
def need_cache_function():
	return 'hello wrapcache'

API方法

  1. wrapcache.keyof(func, *args, **kws): 获取方法的缓存Key值.
  2. wrapcache.get(key, adapter = MemoryAdapter): 或者缓存之.
  3. wrapcache.set(key, value, timeout = -1, adapter = MemoryAdapter): 设置缓存值.
  4. wrapcache.remove(key, adapter = MemoryAdapter): 移除一个缓存.
  5. wrapcache.flush(adapter = MemoryAdapter): 移除所有的缓存.

API方法中,第2~5个API在使用的时候,需要传入 adapter 来设置需要操作哪一个适配器。

TODO

  • 使用文档 wiki.
The MIT License (MIT) Copyright (c) 2016 atool Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A python Function / Method OUTPUT cache system base on function Decorators. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/hustcc/wrapcache.git
git@gitee.com:hustcc/wrapcache.git
hustcc
wrapcache
wrapcache
master

搜索帮助