V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
50vip
V2EX  ›  Python

[首发]元旦撸一个 Python 装饰器的缓存库——wrapcache

  •  
  •   50vip · 2016-01-04 13:34:29 +08:00 · 2620 次点击
    这是一个创建于 3039 天前的主题,其中的信息可能已经有所发展或是发生改变。

    元旦两天搞的,还通宵了一天。

    github 地址:https://github.com/hustcc/wrapcache

    Build Status codecov Status PyPi Status PyPi Downloads

    使用简单,只需要要在方法上面加一个装饰器即可缓存,并且设置缓存过期时间。

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

    以上即可,第一次运行需要 2 秒,第二次运行(过期时间 60 秒之内)瞬间给出缓存结果。适合于小场景的方法缓存。

    安装方法:

    pip install wrapcache
    

    即可,当然你也可以直接下载 github 代码,然后放到自己项目中即可。

    支持 python2 , python3 各版本。

    TODO

    目前是使用内存 dict 存储缓存,后续要支持将 redis 和 memcached 等服务器中,只需要补充 adapter 中代码,实现对应方法即可

    欢迎 push requst 和 issue 。

    4 条回复    2016-01-05 10:53:05 +08:00
    siteshen
        1
    siteshen  
       2016-01-05 00:57:43 +08:00
    各种轮子。。要是 gist 倒有些兴趣用。。
    50vip
        2
    50vip  
    OP
       2016-01-05 10:46:16 +08:00
    @siteshen gist 和 repo 有什么区别了? repo 需要很大?
    50vip
        3
    50vip  
    OP
       2016-01-05 10:46:53 +08:00
    @siteshen repo 也是开源的代码,直接也可以看啊~~~只有 gist 能看?
    50vip
        4
    50vip  
    OP
       2016-01-05 10:53:05 +08:00
    @siteshen http://microjs.com/# 这里全是小轮子~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5235 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:46 · PVG 16:46 · LAX 01:46 · JFK 04:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.