2014-07-10 12 views
9

Ho un'applicazione con Python Bottle e voglio aggiungere Cache-Control nei file statici. Sono nuovo su questo, quindi perdonami se ho fatto qualcosa di sbagliato.Python Bottle and Cache-Control

Qui è la funzione e come servire i file statici:

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

Per aggiungere Cache-Control ho incluso una linea più (l'ho visto in un tutorial)

@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/') 

Ma quando Controllo le intestazioni dagli strumenti di sviluppo su Chrome: ho o Cache-Control:max-age=0 o Cache-Control:no-cache

+0

Prova utilizzando 'response.set_header()' invece di 'response.headers' come si dice nelle [doc] (http: // bottlepy. org/docs/dev/tutorial.html? evidenziare = Cache-Control). Qualcosa come questo 'response.set_header ('Cache-Control', 'max-age = 3600, public')' – doru

+0

@doru L'ho già provato ma nella scheda di rete in Chrome Developer Tools ho la stessa cosa (Cache- controllo: max-age = 0). E ogni file statico sembra caricare in ogni aggiornamento – Sfinos

+0

Prova con 'wget' o' curl' invece di Chrome e facci sapere cosa vedi. –

risposta

14

Ho dato un'occhiata allo source code per static_file() e trovato la soluzione.

È necessario assegnare il risultato di static_file(...) a una variabile e chiamare set_header() sull'oggetto HTTPResponse risultante.

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

Fondamentalmente static_file(...) crea un nuovo marchio HTTPResponse oggetto e la vostra modifica bottle.response non ha alcun effetto qui.

Questo fa preicesly quello che stai dopo:

$ 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

Per chi si chiede, 604800 è il numero di secondi in una settimana. '604800/60/60/24 = 7'. La curiosità è strana, a volte. – tleb

Problemi correlati