Skip to content

Commit

Permalink
Make cache max-age tweakable, add ?ts= cache pattern to /jsapi/
Browse files Browse the repository at this point in the history
  • Loading branch information
BjarniRunar committed Oct 23, 2014
1 parent b26fa2e commit 9654956
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
9 changes: 9 additions & 0 deletions mailpile/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ def command(self):
def etag_data(self):
return []

def max_age(self):
return 0

@classmethod
def view(cls, result):
return result
Expand Down Expand Up @@ -1913,6 +1916,12 @@ class Output(Command):
IS_USER_ACTIVITY = False
LOG_NOTHING = True

def etag_data(self):
return self.get_render_mode()

def max_age(self):
return 364 * 24 * 3600 # A long time!

def get_render_mode(self):
return self.args and self.args[0] or 'text'

Expand Down
12 changes: 9 additions & 3 deletions mailpile/httpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,14 @@ def _real_do_GET(self, post_data={}, suppress_body=False, method='GET'):
cachectrl = None
if 'http' not in config.sys.debug:
etag_data = []
max_ages = []
have_ed = 0
for c in commands:
etag_data.extend(c.etag_data())
if etag_data:
max_ages.append(c.max_age())
ed = c.etag_data()
have_ed += 1 if ed else 0
etag_data.extend(ed)
if have_ed == len(commands):
etag = self._mk_etag(*etag_data)
conditional = self.headers.get('if-none-match')
if conditional == etag:
Expand All @@ -373,7 +378,8 @@ def _real_do_GET(self, post_data={}, suppress_body=False, method='GET'):
return None
else:
http_headers.append(('ETag', etag))
cachectrl = "must-revalidate, max-age=10"
max_age = min(max_ages) if max_ages else 10
cachectrl = 'must-revalidate, max-age=%d' % max_age

global LIVE_HTTP_REQUESTS
hang_fix = 1 if ([1 for c in commands if c.IS_HANGING_ACTIVITY]
Expand Down
9 changes: 9 additions & 0 deletions mailpile/plugins/html_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class JsApi(Command):
ORDER = ('Internals', 0)
HTTP_CALLABLE = ('GET', )
HTTP_AUTH_REQUIRED = 'Maybe'
HTTP_QUERY_VARS = {'ts': 'Cache busting timestamp'}

def max_age(self):
# Set a long TTL if we know which version of the config this request
# applies to, as changed config should avoid the outdated cache.
if 'ts' in self.data:
return 7 * 24 * 3600
else:
return 30

def etag_data(self):
# This summarizes the config state this page depends on, for
Expand Down
8 changes: 4 additions & 4 deletions static/default/html/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<link rel="stylesheet" href="/static/css/default.css" />
<link rel="stylesheet" href="/static/css/print.css" media="print" />
{% if state.command_url not in ("/auth/login/", "/auth/logout/") %}
<link rel="stylesheet" href="/jsapi/as.css" />
<link rel="stylesheet" href="/jsapi/as.css?ts={{ config.version }}-{{ config.timestamp }}" />
{% endif %}

<!-- Apple Icons -->
Expand All @@ -18,7 +18,7 @@
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/libraries.min.js"></script>
{% if state.command_url not in ("/auth/login/", "/auth/logout/", "/setup/welcome/") %}
<script src="/api/0/jsapi/as.js"></script>
<script src="/api/0/jsapi/as.js?ts={{ config.version }}-{{ config.timestamp }}"></script>
{% if state.command_url in ("/message/draft/", "/message/") %}
<script src="/static/js/plupload.full.min.js"></script>
{% endif %}
Expand Down Expand Up @@ -78,6 +78,6 @@
{{ui_elements_setup('.plugin-activity-%(name)s', get_ui_elements('activities', state, '/'))}}
);
</script>
<script src="/api/0/jsapi/app.js"></script>
<script src="/api/0/jsapi/app.js?ts={{ config.version }}-{{ config.timestamp }}"></script>
{% endif %}
{% endif %}
{% endif %}
1 change: 0 additions & 1 deletion static/default/html/partials/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
$(document).ready(function() {
Mailpile.UI.init();
});
</script>

0 comments on commit 9654956

Please sign in to comment.