假设我们有一个基于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=1,http://localhost/?id=2,http://localhost/?id=3等。每页至少需要500毫秒第一次加载。
如何使这些页面的后续加载更快?
更确切地说,有两种方法都可以:
客户端缓存:如果用户A访问过一次http://localhost/?id=1,那么如果用户A第二次访问此页面,它将更快]]
[服务器端缓存:如果用户A访问过一次http://localhost/?id=1,那么如果用户B
?
注意:
在我的代码中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)#这里有些...
服务器端