Basj
Basj

Reputation: 46513

Python bottle server-side caching

Let's say we have an application based on Bottle like this:

from bottle import route, run, request, template, response
import time

def long_processing_task(i):
    time.sleep(0.5)      # here some more 
    return int(i)+2      # complicated processing in reality

@route('/')
def index():
    i = request.params.get('id', '', type=str)
    a = long_processing_task(i)
    response.set_header("Cache-Control", "public, max-age=3600")   # does not seem to work
    return template('Hello {{a}}', a=a)     # here in reality it's: template('index.html', a=a, b=b, ...)  based on an external template file

run(port=80)

Obviously going to http://localhost/?id=1, http://localhost/?id=2, http://localhost/?id=3, etc. takes at least 500 ms per page for the first loading.

How to make subsequent loading of these pages are faster?

More precisely, is there a way to have both:

?

Notes:

but I think it's not related to caching of the final page ready to send to the client.

Upvotes: 0

Views: 998

Answers (1)

ron rothman
ron rothman

Reputation: 18148

Server Side

You want to avoid calling your long running task repeatedly. A naive solution that would work at small scale is to memoize long_processing_task:

from functools import lru_cache

@lru_cache(maxsize=1024)
def long_processing_task(i):
    time.sleep(0.5)      # here some more 
    return int(i)+2      # complicated processing in reality

More complex solutions (that scale better) involve setting up a reverse proxy (cache) in front of your web server.

Client Side

You'll want to use response headers to control how clients cache your responses. (See Cache-Control and Expires headers.) This is a broad topic, with many nuanced alternatives that are out of scope in an SO answer - for example, there are tradeoffs involved in asking clients to cache (they won't get updated results until their local cache expires).

An alternative to caching is to use conditional requests: use an ETag or Last-Modified header to return an HTTP 304 when the client has already received the latest version of the response.

Here's a helpful overview of the various header-based caching strategies.

Upvotes: 1

Related Questions