缓存(假的)静态内容,这对于Python的GAE实际上是动态的 [英] Caching of (fake) static content which is actually dynamic on GAE for Python

查看:99
本文介绍了缓存(假的)静态内容,这对于Python的GAE实际上是动态的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GAE应用程序中,我在app.yaml中有以下处理程序:

   -  url:/lang/strings.js 
script:js_lang.py

因此,调用 / lang /strings.js 实际上会映射到js_lang.py请求处理程序,它将响应填充为 application / javascript 。我希望这个响应被缓存在浏览器中,这样请求处理程序只会被暂时调用一次(例如,当我通过导入 /lang/strings.js?v=xxxx来无效缓存当我部署新版本的应用程序。



对于正常的静态内容,有 default_expiration

 过期时间:星期五,01月4日2011 09:54:56 GMT 
Cache-Control:public,max-age = 600

好的,问题是:有没有简单的方法可以让我返回这样的头文件,而不必明确地设置它们呢?或者,是否有代码片段接受几个基本参数,如days并生成预期的http标题?



编辑2011年4月12日



通过设置两个头 Expires Cache-Control 像这样:

  import datetime 
thirty_days_in_seconds = 4320000
expires_date = datetime.datetime.now()+ datetime.timedelta(days = 30 )
HTTP_HEADER_FORMAT =%a,%d%b%Y%H:%M:00 GMT

self.response.headers [Expires] = expires_date.strftime(HTTP_HEADER_FORMAT )
self.response.headers [Cache-Control] =public,max-age =%s%thirty_days_in_seconds


解决方案

查看 静态服务 博客文章 Nick



您需要了解的所有信息有条件的请求以及如何正确获取并设置正确的HTTP头文件:


  • Http请求头处理
    If-Modified - 自 If-None-Match
  • Http Response headers处理
    Last-Modified ETag

In my GAE app I have the following handler in app.yaml:

- url: /lang/strings.js
  script: js_lang.py

So a call to /lang/strings.js will actually map to the js_lang.py request handler which populates the response as application/javascript. I want this response to be cached in the browser so that the request handler only gets called once in a while (for example when I "invalidate" the cache by importing /lang/strings.js?v=xxxx when I deploy a new version of the app.

For normal static content, there is the default_expiration element, which is very handy. And results in http response headers like this:

Expires: Fri, 01 Apr 2011 09:54:56 GMT
Cache-Control: public, max-age=600

Ok, the question: is there an easy way for me to return headers such as this, without having to explicitly set them? Alternatively, is there a code snippet out there that accepts a few basic parameters such as "days" and produces the expected http-headers?

Edit 12 April 2011

I solved this very by simply setting the two headers Expires and Cache-Control like this:

import datetime
thirty_days_in_seconds = 4320000
expires_date = datetime.datetime.now() + datetime.timedelta(days=30)
HTTP_HEADER_FORMAT = "%a, %d %b %Y %H:%M:00 GMT"        

self.response.headers["Expires"] = expires_date.strftime(HTTP_HEADER_FORMAT)
self.response.headers["Cache-Control"] = "public, max-age=%s" % thirty_days_in_seconds

解决方案

Have a look at Static serving blog post by Nick.

There's everything you need to know about Conditional request and how to properly get and set the correct HTTP headers:

  • Http Request header handling (If-Modified-Since,If-None-Match)
  • Http Response headers handling (Last-Modified, ETag)

这篇关于缓存(假的)静态内容,这对于Python的GAE实际上是动态的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆