author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 03 Feb 2010 14:46:32 +0100 | |
changeset 4436 | 294e084f1263 |
parent 4252 | 6c4f109c2b03 |
parent 4424 | 5a5cd7591706 |
child 5155 | 1dea6e0fdfc1 |
child 5421 | 8167de96c523 |
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 |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
21 |
self.req = view._cw |
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3460
diff
changeset
|
22 |
self.cw_rset = view.cw_rset |
0 | 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): |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
46 |
return self.view.__regid__ + '/' + ','.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): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
82 |
if self.cw_rset is None or len(self.cw_rset) == 0: # entity startup view for instance |
0 | 83 |
return super(EntityHTTPCacheManager, self).etag() |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
84 |
if len(self.cw_rset) > 1: |
0 | 85 |
raise NoEtag() |
86 |
etag = super(EntityHTTPCacheManager, self).etag() |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
87 |
eid = self.cw_rset[0][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: |
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3460
diff
changeset
|
119 |
mtime = self._cw.header_if_modified_since() |
0 | 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 |