Jinja 2.1 和更高的版本支持外部字節碼緩存。字節碼緩存使得在首次使用時把生成的字節碼 存儲到文件系統或其它位置來避免處理模板。
這在當你有一個在首個應用初始化的 web 應用, Jinja 一次性編譯大量模板拖慢應用時尤其 有用。
要使用字節碼緩存,把它實例化并傳給?[Environment](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Environment "jinja2.Environment")?。
*class?*jinja2.BytecodeCache[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.BytecodeCache "Permalink to this definition")
To implement your own bytecode cache you have to subclass this class and override[load_bytecode()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.BytecodeCache.load_bytecode "jinja2.BytecodeCache.load_bytecode")?and?[dump_bytecode()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.BytecodeCache.dump_bytecode "jinja2.BytecodeCache.dump_bytecode"). Both of these methods are passed a?[Bucket](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.bccache.Bucket "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 Jinja2.
clear()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.BytecodeCache.clear "Permalink to this definition")
Clears the cache. This method is not used by Jinja2 but should be implemented to allow applications to clear the bytecode cache used by a particular environment.
dump_bytecode(*bucket*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.BytecodeCache.dump_bytecode "Permalink to this definition")
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.
load_bytecode(*bucket*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.BytecodeCache.load_bytecode "Permalink to this definition")
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.
*class?*jinja2.bccache.Bucket(*environment*,?*key*,?*checksum*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.bccache.Bucket "Permalink to this definition")
Buckets are used to store the bytecode for one template. It’s created and initialized by the bytecode cache and passed to the loading functions.
The buckets get an internal checksum from the cache assigned and use this to automatically reject outdated cache material. Individual bytecode cache subclasses don’t have to care about cache invalidation.
environment[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Bucket.environment "Permalink to this definition")
創建 bucket 的?[Environment](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Environment "jinja2.Environment")
key[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Bucket.key "Permalink to this definition")
該 bucket 的唯一鍵
code[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Bucket.code "Permalink to this definition")
如果已加載,則為字節碼,否則為?None?。
bytecode_from_string(*string*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.bccache.Bucket.bytecode_from_string "Permalink to this definition")
Load bytecode from a string.
bytecode_to_string()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.bccache.Bucket.bytecode_to_string "Permalink to this definition")
Return the bytecode as string.
load_bytecode(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.bccache.Bucket.load_bytecode "Permalink to this definition")
Loads bytecode from a file or file like object.
reset()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.bccache.Bucket.reset "Permalink to this definition")
Resets the bucket (unloads the bytecode).
write_bytecode(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.bccache.Bucket.write_bytecode "Permalink to this definition")
Dump the bytecode into the file or file like object passed.
內建的字節碼緩存:
*class?*jinja2.FileSystemBytecodeCache(*directory=None*,*pattern='__jinja2_%s.cache'*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.FileSystemBytecodeCache "Permalink to this definition")
A bytecode cache that stores bytecode on the filesystem. It accepts two arguments: The directory where the cache items are stored and a pattern string that is used to build the filename.
If no directory is specified the system temporary items folder is used.
The pattern can be used to have multiple separate caches operate on the same directory. The default pattern is?'__jinja2_%s.cache'.?%s?is replaced with the cache key.
~~~
>>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')
~~~
This bytecode cache supports clearing of the cache using the clear method.
*class?*jinja2.MemcachedBytecodeCache(*client*,?*prefix='jinja2/bytecode/'*,*timeout=None*,?*ignore_memcache_errors=True*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.MemcachedBytecodeCache "Permalink to this definition")
This class implements a bytecode cache that uses a memcache cache for storing the information. It does not enforce a specific memcache library (tummy’s memcache or cmemcache) but will accept any class that provides the minimal interface required.
Libraries compatible with this class:
* [werkzeug](http://werkzeug.pocoo.org/).contrib.cache
* [python-memcached](http://www.tummy.com/Community/software/python-memcached/)
* [cmemcache](http://gijsbert.org/cmemcache/)
(Unfortunately the django cache interface is not compatible because it does not support storing binary data, only unicode. You can however pass the underlying cache client to the bytecode cache which is available asdjango.core.cache.cache._client.)
The minimal interface for the client passed to the constructor is this:
*class?*MinimalClientInterface[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.MemcachedBytecodeCache.MinimalClientInterface "Permalink to this definition")
set(*key*,?*value*[,?*timeout*])[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.MemcachedBytecodeCache.MinimalClientInterface.set "Permalink to this definition")
Stores the bytecode in the cache.?value?is a string and?timeout?the timeout of the key. If timeout is not provided a default timeout or no timeout should be assumed, if it’s provided it’s an integer with the number of seconds the cache item should exist.
get(*key*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.MemcachedBytecodeCache.MinimalClientInterface.get "Permalink to this definition")
Returns the value for the cache key. If the item does not exist in the cache the return value must be?None.
The other arguments to the constructor are the prefix for all keys that is added before the actual cache key and the timeout for the bytecode in the cache system. We recommend a high (or no) timeout.
This bytecode cache does not support clearing of used items in the cache. The clear method is a no-operation function.
New in version 2.7:?Added support for ignoring memcache errors through theignore_memcache_errors?parameter.
- 介紹
- 預備知識
- 安裝
- 基本 API 使用
- 實驗性的 Python 3 支持
- API
- 基礎
- Unicode
- 高層 API
- 自動轉義
- 標識符的說明
- 未定義類型
- 上下文
- 加載器
- 字節碼緩存
- 實用工具
- 異常
- 自定義過濾器
- 求值上下文
- 自定義測試
- 全局命名空間
- 低層 API
- 元 API
- 沙箱
- API
- 運算符攔截
- 模板設計者文檔
- 概要
- 變量
- 過濾器
- 測試
- 注釋
- 空白控制
- 轉義
- 行語句
- 模板繼承
- HTML 轉義
- 控制結構清單
- 導入上下文行為
- 表達式
- 內置過濾器清單
- 內置測試清單
- 全局函數清單
- 擴展
- 自動轉義擴展
- 擴展
- 添加擴展
- i18n 擴展
- 表達式語句
- 循環控制
- With 語句
- 自動轉義擴展
- 編寫擴展
- 集成
- Babel 集成
- Pylons
- TextMate
- Vim
- 從其它的模板引擎切換
- Jinja1
- Django
- Mako
- 提示和技巧
- Null-Master 退回
- 交替的行
- 高亮活動菜單項
- 訪問父級循環