2014-07-10 27 views
9

Python Şişesi ile bir uygulamam var ve statik dosyalara Cache-Control eklemek istiyorum. Bu konuda yeniyim, eğer yanlış bir şey yapsam beni affet. İşte Python Şişe ve Önbellek Denetimi

fonksiyonudur ve statik dosyalara hizmet nasıl: I (Ben öğretici gördüm)

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

Ama ne zaman bir daha hattını dahil ettik Cache-Control eklemek için

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

Başlıkları Chrome'daki Geliştirici araçlarından kontrol ediyorum: Cache-Control:max-age=0 veya Cache-Control:no-cache

+0

('()' yerine response.headers' 'ait response.set_header kullanmayı deneyin http: // bottlepy. org/docs/dev/tutorial.html? = cache-kontrolünü vurgulamak). Bunun gibi bir şey response.set_header ('Cache-Control', 'max-age = 3600, public') ' – doru

+0

@doru Zaten denedim ama Chrome Geliştirici Araçları'nda ağ sekmesinde de aynı şey var (Cache- Kontrol: max-age = 0). Her sabit dosya – Sfinos

+0

içinde yenilenir. Lütfen Chrome yerine "wget" veya "curl" ile deneyin ve gördüğünüz içeriği bize bildirin. –

cevap

14

için source code'a bir göz attımve çözümü buldu.

Bir değişkene static_file(...) sonucunu atamanız ve sonuçta HTTPResponse nesnesine set_header() numaralı telefonu çağırmanız gerekir.

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

Temelde static_file(...) yepyeni HTTPResponse nesnesi oluşturur ve bottle.response için modifikasyon burada hiçbir etkisi yoktur.

Bu does preicesly sen sonra ne: Onlar [docs] dedikleri gibi

$ 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

Merak edenler için, haftada 604800 saniyedir. 604800/60/60/24 = 7 '. Merak bazen gariptir. – tleb

İlgili konular