Lars Wiegman

  • Home
  • Skills
  • Contact
  • Blog

Tag cached responses with uWSGI

posted on March 4, 2014 #

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

Read Next:

Dennis the DNS menace

Just pushed a new project to Github, it's just a small piece of a live project as I'm usually not at liberty to open-source production code. The project is called Dennis. It's a nameserver which can serve customised DNS responses on a per user basis. It's written in the Go programming language and uses Redis as a fast datastore. On its own Dennis isn't very useful but by adding a DNS recursor and a HTTP(S) proxy, Dennis can bypass geo-blocking for thousands of users. Continue »

  • Home
  • Skills
  • Blog
  • Contact
  • •
  • Archive
  • Feed
  • GitHub

Copyright © 2007-2024 Lars Wiegman