Python瓶服务器端缓存

问题描述 投票:0回答:1

假设我们有一个基于Bottle的应用程序,如下所示:

from bottle import route, run, request, template, response
import time

def long_processing_task(i):
    time.sleep(0.5)      # here some more 
    return int(i)+2      # complicated processing in reality

@route('/')
def index():
    i = request.params.get('id', '', type=str)
    a = long_processing_task(i)
    response.set_header("Cache-Control", "public, max-age=3600")   # does not seem to work
    return template('Hello {{a}}', a=a)     # here in reality it's: template('index.html', a=a, b=b, ...)  based on an external template file

run(port=80)

显然转到http://localhost/?id=1http://localhost/?id=2http://localhost/?id=3等。每页至少需要500毫秒第一次加载

如何使这些页面的后续加载更快?

更确切地说,有两种方法都可以:

  • 客户端缓存:如果用户A访问过一次http://localhost/?id=1,那么如果用户A第二次访问此页面,它将更快]]

  • [服务器端缓存:如果用户A访问过一次http://localhost/?id=1,那么如果用户B

  • 以后访问了此页面(对于用户B来说是第一次!),它也会更快。] >。换句话说:如果花费500毫秒为一个用户生成http://localhost/?id=1,它将被缓存为请求同一页面的所有将来的用户。 (有一个名字吗?)

    注意:

  • 在我的代码中response.set_header("Cache-Control", "public, max-age=3600")似乎无效。

  • this tutorial中,提到了模板缓存:

    模板在编译后被缓存在内存中。在您清除模板缓存之前,对模板文件所做的修改将不起作用。调用bottle.TEMPLATES.clear()这样做。在调试模式下禁用了缓存。

  • 但是我认为这与准备发送给客户端的最终页面的缓存无关。

    假设我们有一个基于Bottle的应用程序,如下所示:从Bottle导入路径,运行,请求,模板,响应导入时间def long_processing_task(i):time.sleep(0.5)#这里有些...

python caching bottle cache-control
1个回答
1
投票

服务器端

© www.soinside.com 2019 - 2024. All rights reserved.