2 Star 3 Fork 0

阿债 / rdcache

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

cache: caching for humans

Installation

pip install -U anyjson redis rdcache

Usage:

# For memcache
import pylibmc
from rdcache import Cache
backend = pylibmc.Client(["127.0.0.1"])
cache = Cache(backend)

# For Redis
REDIS_CONFS = {
    "default": {
        "host": "127.0.0.1",
        "port": 6379,
        "password": "",
        "db": 0,
        "socket_timeout": 3600,
        "max_connections": 128,
    },
}
from rdcache.ext import RedisCache, RedisPool
redis = RedisPool(REDIS_CONFS)
cache = RedisCache(redis.get('default'), touch = False)


@cache("mykey-%s")
def some_expensive_method(num):
    sleep(10)
    if not isinstance(num, int):
        if isinstance(num, basestring) and num.isdigit():
            num = int(num)
        else:
            num = 0
    return num

# reads 42 from the cache, the key is mykey-42
some_expensive_method(42)

# re-calculates and writes 42 to the cache
some_expensive_method.refresh(42)

# get the cached value or throw an error
# (unless default= was passed to @cache(...))
some_expensive_method.cached(42)

Options

Options can be passed to either the Cache constructor or the decorator. Options passed to the decorator take precedence. Available options are:

enabled    If `False`, the backend cache will not be used at all,
           and your functions will be run as-is, even when you call
           `.cached()`.  This is useful for development, when the
           function may be changing rapidly.
           Default: True

default    If given, `.cached()` will return the given value instead
           of raising a KeyError.
           
type       data/json
           string/json/hash/list/set/zset (if backend is redis)
           Default: data
           
time       expire seconds
           Default: -1 (forever)
            
touch      If true, expire time seconds everytime include reading data 

The remaining options, if given, will be passed as keyword arguments to the backend's set method. This is useful for things like expiration times - for example, using pylibmc:

@cache("some_key_%s_%d", type='json', time=3600)
def expensive_method(name, ver=1):
    # ...

Dummy Cache

Cache provides a "fake" caches for local development without a backend cache: DummyCache.

P.S.

If you're a Ruby user, check out the analogous Cacher library for Ruby

# MIT license. See http://www.opensource.org/licenses/mit-license.php Copyright (c) 2012 Desmos, Inc. 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.

简介

Caching for humans 展开 收起
Python 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/azhai/rdcache.git
git@gitee.com:azhai/rdcache.git
azhai
rdcache
rdcache
master

搜索帮助