Skip to content

Set the cache headers for static resources #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions iris.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 2 additions & 6 deletions qwebirc/engines/staticengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions qwebirc/util/caching.py
Original file line number Diff line number Diff line change
@@ -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