jinja2.bccache.BytecodeCache

class jinja2.bccache.BytecodeCache[source]

To implement your own bytecode cache you have to subclass this class and override load_bytecode() and dump_bytecode(). Both of these methods are passed a Bucket.

A very basic bytecode cache that saves the bytecode on the file system:

from os import path

class MyCache(BytecodeCache):

    def __init__(self, directory):
        self.directory = directory

    def load_bytecode(self, bucket):
        filename = path.join(self.directory, bucket.key)
        if path.exists(filename):
            with open(filename, 'rb') as f:
                bucket.load_bytecode(f)

    def dump_bytecode(self, bucket):
        filename = path.join(self.directory, bucket.key)
        with open(filename, 'wb') as f:
            bucket.write_bytecode(f)

A more advanced version of a filesystem based bytecode cache is part of Jinja2.

Methods

clear() Clears the cache.
dump_bytecode(bucket) Subclasses have to override this method to write the bytecode from a bucket back to the cache.
get_bucket(environment, name, filename, source) Return a cache bucket for the given template.
get_cache_key(name[, filename]) Returns the unique hash key for this template name.
get_source_checksum(source) Returns a checksum for the source.
load_bytecode(bucket) Subclasses have to override this method to load bytecode into a bucket.
set_bucket(bucket) Put the bucket into the cache.