author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Mon, 08 Feb 2010 15:28:34 +0100 | |
branch | stable |
changeset 4505 | a82af6b000f2 |
parent 4424 | 5a5cd7591706 |
child 4436 | 294e084f1263 |
permissions | -rw-r--r-- |
0 | 1 |
"""HTTP cache managers |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
4424
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
11 |
from time import mktime |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
762
diff
changeset
|
12 |
from datetime import datetime |
0 | 13 |
|
14 |
# time delta usable to convert localized time to GMT time |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
762
diff
changeset
|
15 |
GMTOFFSET = - (datetime.now() - datetime.utcnow()) |
0 | 16 |
|
17 |
class NoHTTPCacheManager(object): |
|
18 |
"""default cache manager: set no-cache cache control policy""" |
|
19 |
def __init__(self, view): |
|
20 |
self.view = view |
|
21 |
self.req = view.req |
|
22 |
self.rset = view.rset |
|
23 |
||
24 |
def set_headers(self): |
|
25 |
self.req.set_header('Cache-control', 'no-cache') |
|
26 |
||
4424
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
27 |
|
0 | 28 |
class MaxAgeHTTPCacheManager(NoHTTPCacheManager): |
29 |
"""max-age cache manager: set max-age cache control policy, with max-age |
|
30 |
specified with the `cache_max_age` attribute of the view |
|
31 |
""" |
|
32 |
def set_headers(self): |
|
33 |
self.req.set_header('Cache-control', |
|
34 |
'max-age=%s' % self.view.cache_max_age) |
|
35 |
||
4424
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
36 |
|
0 | 37 |
class EtagHTTPCacheManager(NoHTTPCacheManager): |
38 |
"""etag based cache manager for startup views |
|
39 |
||
40 |
* etag is generated using the view name and the user's groups |
|
41 |
* set policy to 'must-revalidate' and expires to the current time to force |
|
42 |
revalidation on each request |
|
43 |
""" |
|
44 |
||
45 |
def etag(self): |
|
46 |
return self.view.id + '/' + ','.join(sorted(self.req.user.groups)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1149
diff
changeset
|
47 |
|
0 | 48 |
def max_age(self): |
49 |
# 0 to actually force revalidation |
|
50 |
return 0 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1149
diff
changeset
|
51 |
|
0 | 52 |
def last_modified(self): |
4424
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
53 |
"""return view's last modified GMT time""" |
0 | 54 |
return self.view.last_modified() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1149
diff
changeset
|
55 |
|
0 | 56 |
def set_headers(self): |
57 |
req = self.req |
|
58 |
try: |
|
59 |
req.set_header('Etag', '"%s"' % self.etag()) |
|
60 |
except NoEtag: |
|
61 |
self.req.set_header('Cache-control', 'no-cache') |
|
62 |
return |
|
63 |
req.set_header('Cache-control', |
|
64 |
'must-revalidate;max-age=%s' % self.max_age()) |
|
65 |
mdate = self.last_modified() |
|
4424
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
66 |
# use a timestamp, not a formatted raw header, and let |
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
67 |
# the front-end correctly generate it |
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
68 |
# ("%a, %d %b %Y %H:%M:%S GMT" return localized date that |
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
69 |
# twisted don't parse correctly) |
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
70 |
req.set_header('Last-modified', mktime(mdate.timetuple()), raw=False) |
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
71 |
|
0 | 72 |
|
73 |
class EntityHTTPCacheManager(EtagHTTPCacheManager): |
|
74 |
"""etag based cache manager for view displaying a single entity |
|
75 |
||
76 |
* etag is generated using entity's eid, the view name and the user's groups |
|
77 |
* get last modified time from the entity definition (this may not be the |
|
78 |
entity's modification time since a view may include some related entities |
|
79 |
with a modification time to consider) using the `last_modified` method |
|
80 |
""" |
|
81 |
def etag(self): |
|
82 |
if self.rset is None or len(self.rset) == 0: # entity startup view for instance |
|
83 |
return super(EntityHTTPCacheManager, self).etag() |
|
84 |
if len(self.rset) > 1: |
|
85 |
raise NoEtag() |
|
86 |
etag = super(EntityHTTPCacheManager, self).etag() |
|
87 |
eid = self.rset[0][0] |
|
88 |
if self.req.user.owns(eid): |
|
89 |
etag += ',owners' |
|
90 |
return str(eid) + '/' + etag |
|
91 |
||
92 |
||
93 |
class NoEtag(Exception): |
|
94 |
"""an etag can't be generated""" |
|
95 |
||
96 |
__all__ = ('GMTOFFSET', |
|
97 |
'NoHTTPCacheManager', 'MaxAgeHTTPCacheManager', |
|
98 |
'EtagHTTPCacheManager', 'EntityHTTPCacheManager') |
|
99 |
||
100 |
# monkey patching, so view doesn't depends on this module and we have all |
|
101 |
# http cache related logic here |
|
102 |
||
1149 | 103 |
from cubicweb import view as viewmod |
0 | 104 |
|
105 |
def set_http_cache_headers(self): |
|
106 |
self.http_cache_manager(self).set_headers() |
|
1149 | 107 |
viewmod.View.set_http_cache_headers = set_http_cache_headers |
0 | 108 |
|
4424
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
109 |
|
0 | 110 |
def last_modified(self): |
111 |
"""return the date/time where this view should be considered as |
|
112 |
modified. Take care of possible related objects modifications. |
|
113 |
||
114 |
/!\ must return GMT time /!\ |
|
115 |
""" |
|
116 |
# XXX check view module's file modification time in dev mod ? |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
762
diff
changeset
|
117 |
ctime = datetime.utcnow() |
0 | 118 |
if self.cache_max_age: |
119 |
mtime = self.req.header_if_modified_since() |
|
120 |
if mtime: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
762
diff
changeset
|
121 |
tdelta = (ctime - mtime) |
4423
c1850ef961fc
simpler last_modified implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
122 |
if tdelta.days * 24*60*60 + tdelta.seconds <= self.cache_max_age: |
c1850ef961fc
simpler last_modified implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
123 |
return mtime |
0 | 124 |
# mtime = ctime will force page rerendering |
4423
c1850ef961fc
simpler last_modified implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
125 |
return ctime |
1149 | 126 |
viewmod.View.last_modified = last_modified |
0 | 127 |
|
4424
5a5cd7591706
fix spurious http cache bug: sometimes last-modified headers is generated using non-english local, which ends up in a date that twisted can't parse and make it feels the page may be cached while it may not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4423
diff
changeset
|
128 |
|
0 | 129 |
# configure default caching |
1149 | 130 |
viewmod.View.http_cache_manager = NoHTTPCacheManager |
0 | 131 |
# max-age=0 to actually force revalidation when needed |
1149 | 132 |
viewmod.View.cache_max_age = 0 |
0 | 133 |
|
134 |
||
1149 | 135 |
viewmod.EntityView.http_cache_manager = EntityHTTPCacheManager |
0 | 136 |
|
1149 | 137 |
viewmod.StartupView.http_cache_manager = MaxAgeHTTPCacheManager |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1149
diff
changeset
|
138 |
viewmod.StartupView.cache_max_age = 60*60*2 # stay in http cache for 2 hours by default |