def __init__(self, path):
self.path = path
- CachedLoaderMixin.__init__(
+ CachedLoaderMixin.__init__(self,
True, # use memory caching
40, # for up to 40 templates
'/tmp', # additionally save the compiled templates in /tmp
- True # and reload cached templates automatically if changed
+ True, # and reload cached templates automatically if changed
+ 'foo' # optional salt used to keep templates with the same
+ # name in the same cache folder, but from different
+ # loaders. New in Jinja 1.1 and can be omitted.
)
def get_source(self, environment, name, parent):
exist the option `auto_reload` won't have an effect. Also note that the
`check_source_changed` method must not raise an exception if the template
does not exist but return ``-1``. The return value ``-1`` is considered
-"always reload" whereas ``0`` means "do not reload".
\ No newline at end of file
+"always reload" whereas ``0`` means "do not reload".
if p and p[0] != '.']))
-def get_cachename(cachepath, name):
+def get_cachename(cachepath, name, salt=None):
"""
Return the filename for a cached file.
"""
return path.join(cachepath, 'jinja_%s.cache' %
- sha.new('jinja(%s)tmpl' % name).hexdigest())
+ sha.new('jinja(%s|%s)tmpl' %
+ (name, salt or '')).hexdigest())
class LoaderWrapper(object):
Mixin this class to implement simple memory and disk caching.
"""
- def __init__(self, use_memcache, cache_size, cache_folder, auto_reload):
+ def __init__(self, use_memcache, cache_size, cache_folder, auto_reload,
+ cache_salt=None):
if use_memcache:
self.__memcache = CacheDict(cache_size)
else:
self.__auto_reload = False
else:
self.__auto_reload = auto_reload
+ self.__salt = cache_salt
self.__times = {}
self.__lock = Lock()
# mem cache disabled or not cached by now
# try to load if from the disk cache
if tmpl is None and self.__cache_folder is not None:
- cache_fn = get_cachename(self.__cache_folder, name)
+ cache_fn = get_cachename(self.__cache_folder, name, self.__salt)
if last_change is not None:
try:
cache_time = path.getmtime(cache_fn)
``auto_reload`` Set this to `False` for a slightly better
performance. In that case Jinja won't check for
template changes on the filesystem.
+ ``cache_salt`` Optional unique number to not confuse the
+ caching system when caching more than one
+ template loader in the same folder. Defaults
+ to the searchpath. *New in Jinja 1.1*
=================== =================================================
"""
def __init__(self, searchpath, use_memcache=False, memcache_size=40,
- cache_folder=None, auto_reload=True):
+ cache_folder=None, auto_reload=True, cache_salt=None):
+ if cache_salt is None:
+ cache_salt = searchpath
self.searchpath = searchpath
CachedLoaderMixin.__init__(self, use_memcache, memcache_size,
- cache_folder, auto_reload)
+ cache_folder, auto_reload, cache_salt)
def get_source(self, environment, name, parent):
filename = get_template_filename(self.searchpath, name)
template changes on the filesystem. If the
templates are inside of an egg file this won't
have an effect.
+ ``cache_salt`` Optional unique number to not confuse the
+ caching system when caching more than one
+ template loader in the same folder. Defaults
+ to ``package_name + '/' + package_path``.
+ *New in Jinja 1.1*
=================== =================================================
"""
def __init__(self, package_name, package_path, use_memcache=False,
- memcache_size=40, cache_folder=None, auto_reload=True):
+ memcache_size=40, cache_folder=None, auto_reload=True,
+ cache_salt=None):
if resource_filename is None:
raise ImportError('setuptools not found')
self.package_name = package_name
self.package_path = package_path
+ if cache_salt is None:
+ cache_salt = package_name + '/' + package_path
# if we have an loader we probably retrieved it from an egg
# file. In that case don't use the auto_reload!
if auto_reload and getattr(__import__(package_name, '', '', ['']),
'__loader__', None) is not None:
auto_reload = False
CachedLoaderMixin.__init__(self, use_memcache, memcache_size,
- cache_folder, auto_reload)
+ cache_folder, auto_reload, cache_salt)
def get_source(self, environment, name, parent):
name = '/'.join([self.package_path] + [p for p in name.split('/')
``auto_reload`` Set this to `False` for a slightly better
performance. In that case of `getmtime_func`
not being provided this won't have an effect.
+ ``cache_salt`` Optional unique number to not confuse the
+ caching system when caching more than one
+ template loader in the same folder.
=================== =================================================
"""
def __init__(self, loader_func, getmtime_func=None, use_memcache=False,
- memcache_size=40, cache_folder=None, auto_reload=True):
+ memcache_size=40, cache_folder=None, auto_reload=True,
+ cache_salt=None):
# when changing the signature also check the jinja.plugin function
# loader instantiation.
self.loader_func = loader_func
if auto_reload and getmtime_func is None:
auto_reload = False
CachedLoaderMixin.__init__(self, use_memcache, memcache_size,
- cache_folder, auto_reload)
+ cache_folder, auto_reload, cache_salt)
def get_source(self, environment, name, parent):
rv = self.loader_func(name)