From 11e46a297bd1550b3f279445c95b7f5e35012cdc Mon Sep 17 00:00:00 2001 From: megawac Date: Wed, 12 Mar 2014 10:12:50 -0400 Subject: [PATCH 1/2] Set the cache headers for static resources 30 day default --- qwebirc/engines/staticengine.py | 8 ++------ qwebirc/util/caching.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 qwebirc/util/caching.py diff --git a/qwebirc/engines/staticengine.py b/qwebirc/engines/staticengine.py index 087551c..1ed965c 100644 --- a/qwebirc/engines/staticengine.py +++ b/qwebirc/engines/staticengine.py @@ -3,12 +3,7 @@ import qwebirc.util as util import pprint from adminengine import AdminEngineAction - -# TODO, cache gzip stuff -cache = {} -def clear_cache(): - global cache - cache = {} +from qwebirc.util.caching import cache def apply_gzip(request): accept_encoding = request.getHeader('accept-encoding') @@ -29,6 +24,7 @@ def __init__(self, *args, **kwargs): def render(self, request): self.hit(request) + cache(request) request = apply_gzip(request) return static.File.render(self, request) diff --git a/qwebirc/util/caching.py b/qwebirc/util/caching.py new file mode 100644 index 0000000..1ac3111 --- /dev/null +++ b/qwebirc/util/caching.py @@ -0,0 +1,23 @@ +from wsgiref.handlers import format_date_time as format_date +from datetime import date, timedelta +from time import mktime + +''' + Sets the cache headers for a (static resource) request +''' +def cache(request, expires=30, public=True): + #set expires header + expiry = (date.today() + timedelta(expires)).timetuple() + request.responseHeaders.setRawHeaders("expires" , [format_date(mktime(expiry))]) + + #set cache control + cache_control = "max-age=" + str(int(60*60*24*expires)) + if public: + cache_control += ", public" + else: + cache_control += ", private" + request.responseHeaders.setRawHeaders("cache-control", [cache_control]) + + print dir(request) + + return request \ No newline at end of file From 0fa9ddf8d501a9614a7e4a76cbe2771426760686 Mon Sep 17 00:00:00 2001 From: megawac Date: Tue, 8 Apr 2014 11:18:06 -0400 Subject: [PATCH 2/2] Add Cache period option Add an option to set the duration of the cache. 30 day default --- iris.conf.example | 7 +++++++ qwebirc/util/caching.py | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/iris.conf.example b/iris.conf.example index 0732925..98f676b 100644 --- a/iris.conf.example +++ b/iris.conf.example @@ -376,3 +376,10 @@ http_ajax_request_timeout: 30 # engine will be closed after this many seconds, including connections # that haven't started/completed an HTTP request. http_request_timeout: 5 + + +# HTTP Cache Period: period in **days** to request the client to store +# static resources. Sets the Expires and Cache-Control headers +# Note: recompiling static resources (js/css) will override the previous versions +# in clients cache +http_cache_period: 30 diff --git a/qwebirc/util/caching.py b/qwebirc/util/caching.py index 1ac3111..4d64124 100644 --- a/qwebirc/util/caching.py +++ b/qwebirc/util/caching.py @@ -2,10 +2,15 @@ from datetime import date, timedelta from time import mktime +import qwebirc.config as config + ''' Sets the cache headers for a (static resource) request ''' -def cache(request, expires=30, public=True): +def cache(request, expires=None, public=True): + if expires is None: + expires = int(config.tuneback["http_cache_period"]) + #set expires header expiry = (date.today() + timedelta(expires)).timetuple() request.responseHeaders.setRawHeaders("expires" , [format_date(mktime(expiry))]) @@ -17,7 +22,5 @@ def cache(request, expires=30, public=True): else: cache_control += ", private" request.responseHeaders.setRawHeaders("cache-control", [cache_control]) - - print dir(request) return request \ No newline at end of file