Skip to content
Myr

Fixed bytecode cache and added support for memcached (tests still missing)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 03f8823 · Browse files · 4 files+74 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+74 added−10 removed

  • other:+74 added−10 removed

4 files+74 added−10 removed

modified:docs/api.rst+2 added
434 unmodified lines
435Builtin bytecode caches:436437.. autoclass:: jinja2.FileSystemBytecodeCache438439.. autoclass:: jinja2.MemcachedBytecodeCache440441442Utilities443---------444445These helper functions and classes are useful if you add custom filters or
222 unmodified lines
modified:jinja2/__init__.py+2 added-1 removed
39 unmodified lines
40from jinja2.loaders import BaseLoader, FileSystemLoader, PackageLoader, \41 DictLoader, FunctionLoader, PrefixLoader, ChoiceLoader4243# bytecode caches44from jinja2.bccache import BytecodeCache, FileSystemBytecodeCache44from jinja2.bccache import BytecodeCache, FileSystemBytecodeCache, \45 MemcachedBytecodeCache4647# undefined types48from jinja2.runtime import Undefined, DebugUndefined, StrictUndefined49
18 unmodified lines
modified:jinja2/bccache.py+66 added-5 removed
55 unmodified lines
56 if magic != bc_magic:57 self.reset()58 return59 # the source code of the file changed, we need to reload60 checksum = pickle.load_bytecode(f)60 checksum = pickle.load(f)61 if self.checksum != checksum:62 self.reset()63 return64 # now load_bytecode the code. Because marshal is not able to load_bytecode64 # now load the code. Because marshal is not able to load65 # from arbitrary streams we have to work around that66 if isinstance(f, file):67 self.code = marshal.load_bytecode(f)67 self.code = marshal.load(f)68 else:69 self.code = marshal.loads(f.read())7071 def write_bytecode(self, f):72 """Dump the bytecode into the file or file like object passed."""73 if self.code is None:74 raise TypeError('can\'t write empty bucket')75 f.write(bc_magic)76 pickle.write_bytecode(self.checksum, f, 2)76 pickle.dump(self.checksum, f, 2)77 if isinstance(f, file):78 marshal.write_bytecode(self.code, f)78 marshal.dump(self.code, f)79 else:80 f.write(marshal.dumps(self.code))8182 def bytecode_from_string(self, string):
95 unmodified lines
178 same directory. The default pattern is ``'__jinja2_%s.cache'``. ``%s``179 is replaced with the cache key.180181 >>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')182183 This bytecode cache supports clearing of the cache using the clear method.184 """185186 def __init__(self, directory=None, pattern='__jinja2_%s.cache'):187 if directory is None:
31 unmodified lines
219 try:220 remove(path.join(self.directory, filename))221 except OSError:222 pass223224225class MemcachedBytecodeCache(BytecodeCache):226 """This class implements a bytecode cache that uses a memcache cache for227 storing the information. It does not enforce a specific memcache library228 (tummy's memcache or cmemcache) but will accept any class that provides229 the minimal interface required.230231 Libraries compatible with this class:232233 - `werkzeug <http://werkzeug.pocoo.org/>`_.contrib.cache234 - `python-memcached <http://www.tummy.com/Community/software/python-memcached/>`_235 - `cmemcache <http://gijsbert.org/cmemcache/>`_236237 (Unfortunately the django cache interface is not compatible because it238 does not support storing binary data, only unicode. You can however pass239 the underlaying cache client to the bytecode cache which is available240 as `django.core.cache.cache._client`.)241242 The minimal interface for the client passed to the constructor is this:243244 .. class:: MinimalClientInterface245246 .. method:: set(key, value[, timeout])247248 Stores the bytecode in the cache. `value` is a string and249 `timeout` the timeout of the key. If timeout is not provided250 a default timeout or no timeout should be assumed, if it's251 provided it's an integer with the number of seconds the cache252 item should exist.253254 .. method:: get(key)255256 Returns the value for the cache key. If the item does not257 exist in the cache the return value must be `None`.258259 The other arguments to the constructor are the prefix for all keys that260 is added before the actual cache key and the timeout for the bytecode in261 the cache system. We recommend a high (or no) timeout.262263 This bytecode cache does not support clearing of used items in the cache.264 The clear method is a no-operation function.265 """266267 def __init__(self, client, prefix='jinja2/bytecode/', timeout=None):268 self.client = client269 self.prefix = prefix270 self.timeout = timeout271272 def load_bytecode(self, bucket):273 code = self.client.get(self.prefix + bucket.key)274 if code is not None:275 bucket.bytecode_from_string(code)276277 def dump_bytecode(self, bucket):278 args = (self.prefix + bucket.key, bucket.bytecode_to_string())279 if self.timeout is not None:280 args += (self.timeout,)281 self.client.set(*args)
modified:jinja2/loaders.py+4 added-4 removed
94 unmodified lines
95 source, filename, uptodate = self.get_source(environment, name)9697 # try to load the code from the bytecode cache if there is a98 # bytecode cache configured.99 bbc = environment.bytecode_cache100 if bbc is not None:99 bcc = environment.bytecode_cache100 if bcc is not None:101 bucket = bcc.get_bucket(environment, name, filename, source)102 code = bucket.code103104 # if we don't have code so far (not cached, no longer up to
3 unmodified lines
108109 # if the bytecode cache is available and the bucket doesn't110 # have a code so far, we give the bucket the new code and put111 # it back to the bytecode cache.112 if bbc is not None and bucket.code is None:112 if bcc is not None and bucket.code is None:113 bucket.code = code114 bbc.set_bucket(bucket)114 bcc.set_bucket(bucket)115116 return environment.template_class.from_code(environment, code,117 globals, uptodate)118
181 unmodified lines