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/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..4d64124 --- /dev/null +++ b/qwebirc/util/caching.py @@ -0,0 +1,26 @@ +from wsgiref.handlers import format_date_time as format_date +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=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))]) + + #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]) + + return request \ No newline at end of file