author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 21 Aug 2009 15:03:00 +0200 | |
branch | stable |
changeset 2960 | 1c6eafc68586 |
parent 1977 | 606923dff11b |
child 3451 | 6b46d73823f5 |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
"""HTTP cache managers |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 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 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
762
diff
changeset
|
11 |
from datetime import datetime |
0 | 12 |
|
13 |
# 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
|
14 |
GMTOFFSET = - (datetime.now() - datetime.utcnow()) |
0 | 15 |
|
16 |
class NoHTTPCacheManager(object): |
|
17 |
"""default cache manager: set no-cache cache control policy""" |
|
18 |
def __init__(self, view): |
|
19 |
self.view = view |
|
20 |
self.req = view.req |
|
21 |
self.rset = view.rset |
|
22 |
||
23 |
def set_headers(self): |
|
24 |
self.req.set_header('Cache-control', 'no-cache') |
|
25 |
||
26 |
class MaxAgeHTTPCacheManager(NoHTTPCacheManager): |
|
27 |
"""max-age cache manager: set max-age cache control policy, with max-age |
|
28 |
specified with the `cache_max_age` attribute of the view |
|
29 |
""" |
|
30 |
def set_headers(self): |
|
31 |
self.req.set_header('Cache-control', |
|
32 |
'max-age=%s' % self.view.cache_max_age) |
|
33 |
||
34 |
class EtagHTTPCacheManager(NoHTTPCacheManager): |
|
35 |
"""etag based cache manager for startup views |
|
36 |
||
37 |
* etag is generated using the view name and the user's groups |
|
38 |
* set policy to 'must-revalidate' and expires to the current time to force |
|
39 |
revalidation on each request |
|
40 |
""" |
|
41 |
# GMT time required |
|
42 |
date_format = "%a, %d %b %Y %H:%M:%S GMT" |
|
43 |
||
44 |
def etag(self): |
|
45 |
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
|
46 |
|
0 | 47 |
def max_age(self): |
48 |
# 0 to actually force revalidation |
|
49 |
return 0 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1149
diff
changeset
|
50 |
|
0 | 51 |
def last_modified(self): |
52 |
return self.view.last_modified() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1149
diff
changeset
|
53 |
|
0 | 54 |
def set_headers(self): |
55 |
req = self.req |
|
56 |
try: |
|
57 |
req.set_header('Etag', '"%s"' % self.etag()) |
|
58 |
except NoEtag: |
|
59 |
self.req.set_header('Cache-control', 'no-cache') |
|
60 |
return |
|
61 |
req.set_header('Cache-control', |
|
62 |
'must-revalidate;max-age=%s' % self.max_age()) |
|
63 |
mdate = self.last_modified() |
|
64 |
req.set_header('Last-modified', mdate.strftime(self.date_format)) |
|
65 |
||
66 |
class EntityHTTPCacheManager(EtagHTTPCacheManager): |
|
67 |
"""etag based cache manager for view displaying a single entity |
|
68 |
||
69 |
* etag is generated using entity's eid, the view name and the user's groups |
|
70 |
* get last modified time from the entity definition (this may not be the |
|
71 |
entity's modification time since a view may include some related entities |
|
72 |
with a modification time to consider) using the `last_modified` method |
|
73 |
""" |
|
74 |
def etag(self): |
|
75 |
if self.rset is None or len(self.rset) == 0: # entity startup view for instance |
|
76 |
return super(EntityHTTPCacheManager, self).etag() |
|
77 |
if len(self.rset) > 1: |
|
78 |
raise NoEtag() |
|
79 |
etag = super(EntityHTTPCacheManager, self).etag() |
|
80 |
eid = self.rset[0][0] |
|
81 |
if self.req.user.owns(eid): |
|
82 |
etag += ',owners' |
|
83 |
return str(eid) + '/' + etag |
|
84 |
||
85 |
||
86 |
class NoEtag(Exception): |
|
87 |
"""an etag can't be generated""" |
|
88 |
||
89 |
__all__ = ('GMTOFFSET', |
|
90 |
'NoHTTPCacheManager', 'MaxAgeHTTPCacheManager', |
|
91 |
'EtagHTTPCacheManager', 'EntityHTTPCacheManager') |
|
92 |
||
93 |
# monkey patching, so view doesn't depends on this module and we have all |
|
94 |
# http cache related logic here |
|
95 |
||
1149 | 96 |
from cubicweb import view as viewmod |
0 | 97 |
|
98 |
def set_http_cache_headers(self): |
|
99 |
self.http_cache_manager(self).set_headers() |
|
1149 | 100 |
viewmod.View.set_http_cache_headers = set_http_cache_headers |
0 | 101 |
|
102 |
def last_modified(self): |
|
103 |
"""return the date/time where this view should be considered as |
|
104 |
modified. Take care of possible related objects modifications. |
|
105 |
||
106 |
/!\ must return GMT time /!\ |
|
107 |
""" |
|
108 |
# 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
|
109 |
ctime = datetime.utcnow() |
0 | 110 |
if self.cache_max_age: |
111 |
mtime = self.req.header_if_modified_since() |
|
112 |
if mtime: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
762
diff
changeset
|
113 |
tdelta = (ctime - mtime) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
762
diff
changeset
|
114 |
if tdelta.days * 24*60*60 + tdelta.seconds > self.cache_max_age: |
0 | 115 |
mtime = ctime |
116 |
else: |
|
117 |
mtime = ctime |
|
118 |
else: |
|
119 |
mtime = ctime |
|
120 |
# mtime = ctime will force page rerendering |
|
121 |
return mtime |
|
1149 | 122 |
viewmod.View.last_modified = last_modified |
0 | 123 |
|
124 |
# configure default caching |
|
1149 | 125 |
viewmod.View.http_cache_manager = NoHTTPCacheManager |
0 | 126 |
# max-age=0 to actually force revalidation when needed |
1149 | 127 |
viewmod.View.cache_max_age = 0 |
0 | 128 |
|
129 |
||
1149 | 130 |
viewmod.EntityView.http_cache_manager = EntityHTTPCacheManager |
0 | 131 |
|
1149 | 132 |
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
|
133 |
viewmod.StartupView.cache_max_age = 60*60*2 # stay in http cache for 2 hours by default |