Skip to main content

Optimize Datasette performance behind a caching proxy

Project description

datasette-hashed-urls

PyPI Changelog Tests License

Optimize Datasette performance behind a caching proxy

When you open a database file in immutable mode using the -i option, Datasette calculates a SHA-256 hash of the contents of that file on startup.

This content hash can then optionally be used to create URLs that are guaranteed to change if the contents of the file changes in the future.

The result is pages that can be cached indefinitely by both browsers and caching proxies - providing a significant performance boost.

Demo

A demo of this plugin is running at https://datasette-hashed-urls.vercel.app/

Installation

Install this plugin in the same environment as Datasette.

$ datasette install datasette-hashed-urls

Usage

Once installed, this plugin will act on any immutable database files that are loaded into Datasette:

datasette -i fixtures.db

The database will automatically be renamed to incorporate a hash of the contents of the SQLite file - so the above database would be served as:

http://127.0.0.1:8001/fixtures-aa7318b

Every page that accesss that database, including JSON endpoints, will be served with the following far-future cache expiry header:

cache-control: max-age=31536000, public

Here max-age=31536000 is the number of seconds in a year.

A caching proxy such as Cloudflare can then be used to cache and accelerate content served by Datasette.

When the database file is updated and the server is restarted, the hash will change and content will be served from a new URL. Any hits to the previous hashed URLs will be automatically redirected.

Configuration

You can use the max_age plugin configuration setting to change the cache duration specified in the cache-control HTTP header.

To set the cache expiry time to one hour you would add this to your Datasette metadata.json configuration file:

{
    "plugins": {
        "datasette-hashed-urls": {
            "max_age": 3600
        }
    }
}

History

This functionality used to ship as part of Datasette itself, as a feature called Hashed URL mode.

That feature has been deprecated and will be removed in Datasette 1.0. This plugin should be used as an alternative.

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-hashed-urls
python3 -mvenv venv
source venv/bin/activate

Or if you are using pipenv:

pipenv shell

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datasette-hashed-urls-0.3.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

datasette_hashed_urls-0.3.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file datasette-hashed-urls-0.3.1.tar.gz.

File metadata

  • Download URL: datasette-hashed-urls-0.3.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for datasette-hashed-urls-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f7a22b280434ca2efd4676273e28bb689815ffe46c22e2d474a8882cf69a3668
MD5 8a17d72278c7bdbcdbc1b5420c5b12f8
BLAKE2b-256 d84abd89da15a7f2f5c148241a5dcf675efb7f05eedb829866eaab0ee31485e2

See more details on using hashes here.

File details

Details for the file datasette_hashed_urls-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: datasette_hashed_urls-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for datasette_hashed_urls-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 818e70ab4f2105d5c42d92e452ea03cb4d26e126cc74a7c925320ef07d4b0c55
MD5 6d55354657df46a125619ee671e0b409
BLAKE2b-256 cce5b4aab8d79a3ec5c236bd8add1eaa1468ff62b9f2a7d0fadf816863947d7c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page