2014-07-10 19 views
9

Tôi có một ứng dụng với Python Bottle và tôi muốn thêm Cache-Control trong các tệp tĩnh. Tôi mới về điều này để tha thứ cho tôi nếu tôi đã làm điều gì đó sai trái.Chai Python và Cache-Control

Dưới đây là chức năng và làm thế nào tôi phục vụ các tập tin tĩnh:

@bottle.get('/static/js/<filename:re:.*\.js>') 
def javascripts(filename): 
    return bottle.static_file(filename, root='./static/js/') 

Để thêm Cache-Control tôi đã bao gồm một dòng hơn (tôi thấy nó trong một hướng dẫn)

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

Nhưng khi Tôi kiểm tra tiêu đề từ Công cụ dành cho nhà phát triển trên Chrome: Tôi có hoặc là Cache-Control:max-age=0 hoặc Cache-Control:no-cache

+0

Hãy thử sử dụng 'response.set_header()' thay vì 'response.headers' như họ nói trong [tài liệu] (http: // bottlepy. org/docs/dev/tutorial.html? highlight = cache-control). Một cái gì đó như thế này 'response.set_header ('Cache-Control', 'max-age = 3600, public')' – doru

+0

@doru Tôi đã thử nó nhưng trong tab mạng trong Chrome Developer Tools Tôi có cùng một thứ (Cache- Điều khiển: max-age = 0). Và mọi tệp tĩnh dường như tải trong mỗi lần làm mới – Sfinos

+0

Vui lòng thử với 'wget' hoặc' curl' thay vì Chrome và cho chúng tôi biết những gì bạn thấy. –

Trả lời

14

Tôi đã xem source code cho static_file() và tìm ra giải pháp.

Bạn cần chỉ định kết quả của static_file(...) cho một biến và gọi set_header() trên đối tượng HTTPResponse kết quả.

#!/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) 

Về cơ bản static_file(...) tạo ra một thương hiệu mới HTTPResponse đối tượng và thay đổi của bạn để bottle.response không có tác dụng ở đây.

này không preicesly gì bạn đang sau:

$ 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 
+1

Đối với những người tự hỏi, 604800 là số giây trong một tuần. '604800/60/60/24 = 7'. Sự tò mò là lạ, đôi khi. – tleb

Các vấn đề liên quan