用烧杯缓存 [英] Caching with beaker

查看:91
本文介绍了用烧杯缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以抓取网站数据.我希望能够缓存该数据而不是将其加载(如果自上次检索以来仅几分钟).我看了烧杯,但对缓存我还是很陌生,不知道这是否是我所需要的.我也不太了解Cachemanager是什么,为什么我只使用"cache.get"而不是同时使用"cache.set"和"cache.get".我已经包括了我一直用来测试的脚本.

I have a program that scrapes a website for data. I want to be able to cache that data instead of loading it if its only been a few minutes since it was last retrieved. I looked at beaker but I'm extremely new to cache and not sure if this is what i need. I also do not really understand what the Cachemanager is and why i only use "cache.get" instead of using both "cache.set" and "cache.get". I have included the script that i have been using to test with.

from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
import sched, time
from datetime import datetime

cache_opts = {
             'cache.type': 'file',
             'cache.data_dir': '../Beaker/tmp/cache/data',
             'cache.lock_dir': '../Beaker/tmp/cache/lock'
             }

cache = CacheManager(**parse_cache_config_options(cache_opts))
tmpl_cache = cache.get_cache('mytemplate', type='file', expire=5)

def get_results():
    # do something to retrieve data
    print 'hey'
    data = datetime.now()
    return data

def get_results2():
    return 'askdjfla;j'

s = sched.scheduler(time.time, time.sleep)
def get_time(sc):     
    results = tmpl_cache.get(key='gophers', createfunc=get_results)    
    results2 = tmpl_cache.get(key='hank', createfunc=get_results2)   
    print results,results2
    sc.enter(1, 1, get_time, (sc,))

s.enter(1, 1, get_time, (s,))
s.run()

我要这样做正确吗?

推荐答案

您仅使用cache.get,这是正确的,因为如果在缓存中找不到它,它将调用函数来创建它.如果您改为使用装饰器API,这将变得更加清晰和容易:

You are using only cache.get, and that is correct, because if it isn't found in the cache, it will call the function to create it. This becomes clearer and easier if you instead use the decorator API:

@cache.cache('gophers', expire=3600)
def get_results():
    # do something to retrieve data
    print 'hey'
    data = datetime.now()
    return data

@cache.cache('hank', expire=3600)
def get_results2():
    return 'askdjfla;j'

s = sched.scheduler(time.time, time.sleep)
def get_time(sc):     
    results = get_results()
    results2 = get_results2()
    print results,results2
    sc.enter(1, 1, get_time, (sc,))

这篇关于用烧杯缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆