Skip to content

Instantly share code, notes, and snippets.

@diox
Created April 8, 2011 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save diox/909599 to your computer and use it in GitHub Desktop.
Save diox/909599 to your computer and use it in GitHub Desktop.
How django_compressor {% compress %} works behind the scenes

How django_compressor {% compress %} works behind the scenes

This document assumes you already have an up and running instance of django_compressor, and that you understand how to use it in your templates. The goal is to explain what the main template tag, {% compress %}, does behind the scenes, to help you debug performance problems for instance.

First step: Offline cache

The first thing {% compress %} tries to do is get the offline cache for its nodelist if offline cache is activated. It doesn't parse, doesn't check the modified times of the files, doesn't even know which files are concerned actually, since it doesn't look inside the nodelist. The cache should return the HTML containing the link/script element for the combined file (which, if the cache key exists, is supposed to already exist on disk/custom storage)

Everything stops here if the cache entry exists.

Second step: parsing and file list

A compressor instance is created, which in turns instanciates the HTML parser. The parser is used to determine a file list. Each file mtime is checked, first in cache and then on disk/storage, and this is used to determine an unique cache key.

Third step: Checking the "main" cache

Compressor checks if it can get some info about the combined file corresponding to its instance, using the cache key obtained in the previous step. The cache content here will actually be the HTML contening the final link/script element, just like in the offline step before.

Everything stops here if the cache entry exists.

Fourth step: Generating the combined file if needed

The file is generated if necessary. All filters are executed, and a hash is determined from the contents. This in turns helps determine the file name, which is only saved if it didn't exist already. Then the HTML output is returned (and also saved in the cache). And that's it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment