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
Provenance
- Commit
aa1d17d- Tree
1e73b9d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
03f8823
4 files changed+74 added−10 removed
modified:docs/api.rst+2 added
434 unmodified lines435 Builtin bytecode caches:436 437 .. autoclass:: jinja2.FileSystemBytecodeCache438 439+.. autoclass:: jinja2.MemcachedBytecodeCache440 441+442 Utilities443 ---------444 445 These helper functions and classes are useful if you add custom filters or222 unmodified linesmodified:jinja2/__init__.py+2 added-1 removed
39 unmodified lines40 from jinja2.loaders import BaseLoader, FileSystemLoader, PackageLoader, \41 DictLoader, FunctionLoader, PrefixLoader, ChoiceLoader42 43 # bytecode caches44-from jinja2.bccache import BytecodeCache, FileSystemBytecodeCache44+from jinja2.bccache import BytecodeCache, FileSystemBytecodeCache, \45+ MemcachedBytecodeCache46 47 # undefined types48 from jinja2.runtime import Undefined, DebugUndefined, StrictUndefined49 18 unmodified linesmodified:jinja2/bccache.py+66 added-5 removed
55 unmodified lines56 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())70 71 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))81 82 def bytecode_from_string(self, string):95 unmodified lines178 same directory. The default pattern is ``'__jinja2_%s.cache'``. ``%s``179 is replaced with the cache key.180 181 >>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')182+183+ This bytecode cache supports clearing of the cache using the clear method.184 """185 186 def __init__(self, directory=None, pattern='__jinja2_%s.cache'):187 if directory is None:31 unmodified lines219 try:220 remove(path.join(self.directory, filename))221 except OSError:222 pass223+224+225+class 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.230+231+ Libraries compatible with this class:232+233+ - `werkzeug <http://werkzeug.pocoo.org/>`_.contrib.cache234+ - `python-memcached <http://www.tummy.com/Community/software/python-memcached/>`_235+ - `cmemcache <http://gijsbert.org/cmemcache/>`_236+237+ (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`.)241+242+ The minimal interface for the client passed to the constructor is this:243+244+ .. class:: MinimalClientInterface245+246+ .. method:: set(key, value[, timeout])247+248+ 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.253+254+ .. method:: get(key)255+256+ Returns the value for the cache key. If the item does not257+ exist in the cache the return value must be `None`.258+259+ 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.262+263+ This bytecode cache does not support clearing of used items in the cache.264+ The clear method is a no-operation function.265+ """266+267+ def __init__(self, client, prefix='jinja2/bytecode/', timeout=None):268+ self.client = client269+ self.prefix = prefix270+ self.timeout = timeout271+272+ 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)276+277+ 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 lines95 source, filename, uptodate = self.get_source(environment, name)96 97 # 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.code103 104 # if we don't have code so far (not cached, no longer up to3 unmodified lines108 109 # 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)115 116 return environment.template_class.from_code(environment, code,117 globals, uptodate)118 181 unmodified lines