class documentation

class BytecodeCache:

Known subclasses: jinja2.bccache.FileSystemBytecodeCache, jinja2.bccache.MemcachedBytecodeCache

View In Hierarchy

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 ~jinja2.bccache.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 Jinja.

Method clear Clears the cache. This method is not used by Jinja but should be implemented to allow applications to clear the bytecode cache used by a particular environment.
Method dump​_bytecode Subclasses have to override this method to write the bytecode from a bucket back to the cache. If it unable to do so it must not fail silently but raise an exception.
Method get​_bucket Return a cache bucket for the given template. All arguments are mandatory but filename may be None.
Method get​_cache​_key Returns the unique hash key for this template name.
Method get​_source​_checksum Returns a checksum for the source.
Method load​_bytecode Subclasses have to override this method to load bytecode into a bucket. If they are not able to find code in the cache for the bucket, it must not do anything.
Method set​_bucket Put the bucket into the cache.
def clear(self):
Clears the cache. This method is not used by Jinja but should be implemented to allow applications to clear the bytecode cache used by a particular environment.
def dump_bytecode(self, bucket):
Subclasses have to override this method to write the bytecode from a bucket back to the cache. If it unable to do so it must not fail silently but raise an exception.
Parameters
bucket:BucketUndocumented
def get_bucket(self, environment, name, filename, source):
Return a cache bucket for the given template. All arguments are mandatory but filename may be None.
Parameters
environment:EnvironmentUndocumented
name:strUndocumented
filename:t.Optional[str]Undocumented
source:strUndocumented
Returns
BucketUndocumented
def get_cache_key(self, name, filename=None):
Returns the unique hash key for this template name.
Parameters
name:strUndocumented
filename:t.Optional[t.Union[str]]Undocumented
Returns
strUndocumented
def get_source_checksum(self, source):
Returns a checksum for the source.
Parameters
source:strUndocumented
Returns
strUndocumented
def load_bytecode(self, bucket):
Subclasses have to override this method to load bytecode into a bucket. If they are not able to find code in the cache for the bucket, it must not do anything.
Parameters
bucket:BucketUndocumented
def set_bucket(self, bucket):
Put the bucket into the cache.
Parameters
bucket:BucketUndocumented