Tag cached responses with uWSGI
uWSGI is a powerful application server but the documentation can be light on some subjects like caching.
If you want distinguish cache hits from misses you can use the incache:key=
condition. Using the following uWSGI configuration will add a X-Cache
header to each response.
[uwsgi]
...
; Enable caching
mime-file = /etc/mime.types
cache2 = name=%n-cache,items=100
; Check if URI is cached
route-if = incache:key=${REQUEST_URI},name=%n-cache goto:cache_hit
; Cache MISS
route-label = cache_miss
route-run = addheader:X-Cache: MISS
route-run = cachestore:key=${REQUEST_URI},name=%n-cache,expires=172800
route-run = last:
; Cache HIT
route-label = cache_hit
route-run = addheader:X-Cache: HIT
route-run = cache:key=${REQUEST_URI},name=%n-cache,mime=1
Requesting the same URI twice in a row will tag the response accordingly and add an additional Expires header.
$ curl -I -X GET http://larsman.nl/
HTTP/1.1 200 OK
...
X-Cache: MISS
$ curl -I -X GET http://larsman.nl/
HTTP/1.1 200 OK
...
Expires: Thu, 06 Mar 2014 11:11:14 GMT
X-Cache: HIT