Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python瓶子和缓存控制_Python_Caching_Bottle - Fatal编程技术网

Python瓶子和缓存控制

Python瓶子和缓存控制,python,caching,bottle,Python,Caching,Bottle,我有一个使用Python瓶子的应用程序,我想在静态文件中添加缓存控制。我是新手,所以如果我做错了什么,请原谅我 以下是我如何提供静态文件的功能和方法: @bottle.get('/static/js/<filename:re:.*\.js>') def javascripts(filename): return bottle.static_file(filename, root='./static/js/') @battle.get('/static/js/')) def j

我有一个使用Python瓶子的应用程序,我想在静态文件中添加缓存控制。我是新手,所以如果我做错了什么,请原谅我

以下是我如何提供静态文件的功能和方法:

@bottle.get('/static/js/<filename:re:.*\.js>')
def javascripts(filename):
   return bottle.static_file(filename, root='./static/js/')
@battle.get('/static/js/'))
def javascripts(文件名):
return battle.static_文件(文件名,root='./static/js/'))
为了添加缓存控件,我又增加了一行(我在教程中看到了它)

@battle.get('/static/js/'))
def javascripts(文件名):
bottle.response.headers['Cache-Control']='public,max age=604800'
return battle.static_文件(文件名,root='./static/js/'))
但是当我检查Chrome上开发者工具的标题时:我有
缓存控制:max age=0
缓存控制:无缓存

我查看了for
静态文件()
,找到了解决方案

您需要将
static_file(…)
的结果分配给一个变量,并在生成的
HTTPResponse
对象上调用
set_header()

#!/usr/bin/env python

import bottle


@bottle.get("/static/js/<filename:re:.*\.js>")
def javascripts(filename):
    response = bottle.static_file(filename, root="./static/js/")
    response.set_header("Cache-Control", "public, max-age=604800")
    return response

bottle.run(host="localhost", port=8000, debug=True)

请尝试使用
response.set_header()
而不是在中所说的
response.headers
。类似这样的
响应。set_header('Cache-Control','max age=3600,public')
@doru我已经尝试过了,但在Chrome开发者工具的网络选项卡中,我有相同的东西(Cache-Control:max-age=0)。每个静态文件似乎都会在每次刷新中加载。请尝试使用
wget
curl
而不是Chrome,并让我们知道您看到了什么。@ron.rothman我没有wget,也没有卷发器上的curl。我通过Python和urllib2发出了一个头请求,结果如下:
{'date':'Fri,2014年7月11日22:39:58 GMT','content length':'30458','content type':'text/html;charset=UTF-8','server':'WSGIServer/0.1 Python/2.7.6'}
您注意到响应中没有
缓存控制
头吗?对于那些想知道的人,604800是一周的秒数<代码>604800/60/60/24=7
。好奇心有时很奇怪。
#!/usr/bin/env python

import bottle


@bottle.get("/static/js/<filename:re:.*\.js>")
def javascripts(filename):
    response = bottle.static_file(filename, root="./static/js/")
    response.set_header("Cache-Control", "public, max-age=604800")
    return response

bottle.run(host="localhost", port=8000, debug=True)
$ curl -v -o - http://localhost:8000/static/js/test.js
* Adding handle: conn: 0x7f8ffa003a00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f8ffa003a00) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8000 (#0)
*   Trying ::1...
*   Trying fe80::1...
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /static/js/test.js HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8000
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Tue, 15 Jul 2014 00:19:11 GMT
< Server: WSGIServer/0.1 Python/2.7.6
< Last-Modified: Tue, 15 Jul 2014 00:07:22 GMT
< Content-Length: 69
< Content-Type: application/javascript
< Accept-Ranges: bytes
< Cache-Control: public, max-age=604800
<
$(document).ready(function () {
    console.log("Hello World!");
});
* Closing connection 0