2020-01-10 21:42:02 +00:00
|
|
|
"""The optional bytecode cache system. This is useful if you have very
|
|
|
|
complex template situations and the compilation of all those templates
|
|
|
|
slows down your application too much.
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2020-01-10 21:42:02 +00:00
|
|
|
Situations where this is useful are often forking web applications that
|
|
|
|
are initialized on the first request.
|
2008-09-17 14:19:46 +00:00
|
|
|
"""
|
2014-01-10 10:40:51 +00:00
|
|
|
import errno
|
2020-01-09 20:03:07 +00:00
|
|
|
import fnmatch
|
2020-01-28 06:05:00 +00:00
|
|
|
import marshal
|
2020-01-09 20:03:07 +00:00
|
|
|
import os
|
2020-01-28 06:05:00 +00:00
|
|
|
import pickle
|
2020-01-09 20:03:07 +00:00
|
|
|
import stat
|
|
|
|
import sys
|
2008-09-17 19:28:01 +00:00
|
|
|
import tempfile
|
2021-05-08 20:03:15 +00:00
|
|
|
import typing as t
|
2013-05-19 13:16:13 +00:00
|
|
|
from hashlib import sha1
|
2020-01-28 06:05:00 +00:00
|
|
|
from io import BytesIO
|
2021-05-08 20:03:15 +00:00
|
|
|
from types import CodeType
|
|
|
|
|
|
|
|
if t.TYPE_CHECKING:
|
|
|
|
import typing_extensions as te
|
|
|
|
from .environment import Environment
|
|
|
|
|
|
|
|
class _MemcachedClient(te.Protocol):
|
|
|
|
def get(self, key: str) -> bytes:
|
|
|
|
...
|
|
|
|
|
|
|
|
def set(self, key: str, value: bytes, timeout: t.Optional[int] = None) -> None:
|
|
|
|
...
|
2020-01-28 06:05:00 +00:00
|
|
|
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2020-01-29 05:48:37 +00:00
|
|
|
bc_version = 5
|
2020-01-27 06:22:11 +00:00
|
|
|
# Magic bytes to identify Jinja bytecode cache files. Contains the
|
|
|
|
# Python major and minor version to avoid loading incompatible bytecode
|
|
|
|
# if a project upgrades its Python version.
|
2020-01-10 15:46:18 +00:00
|
|
|
bc_magic = (
|
2020-01-27 06:22:11 +00:00
|
|
|
b"j2"
|
2020-01-10 15:46:18 +00:00
|
|
|
+ pickle.dumps(bc_version, 2)
|
2020-01-27 06:22:11 +00:00
|
|
|
+ pickle.dumps((sys.version_info[0] << 24) | sys.version_info[1], 2)
|
2020-01-10 15:46:18 +00:00
|
|
|
)
|
2008-09-17 14:19:46 +00:00
|
|
|
|
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
class Bucket:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""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.
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2008-09-17 19:28:01 +00:00
|
|
|
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.
|
2008-09-17 14:19:46 +00:00
|
|
|
"""
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __init__(self, environment: "Environment", key: str, checksum: str) -> None:
|
2008-09-17 14:19:46 +00:00
|
|
|
self.environment = environment
|
|
|
|
self.key = key
|
|
|
|
self.checksum = checksum
|
|
|
|
self.reset()
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def reset(self) -> None:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""Resets the bucket (unloads the bytecode)."""
|
2021-05-08 20:03:15 +00:00
|
|
|
self.code: t.Optional[CodeType] = None
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def load_bytecode(self, f: t.BinaryIO) -> None:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""Loads bytecode from a file or file like object."""
|
2008-09-17 14:19:46 +00:00
|
|
|
# make sure the magic header is correct
|
|
|
|
magic = f.read(len(bc_magic))
|
|
|
|
if magic != bc_magic:
|
|
|
|
self.reset()
|
|
|
|
return
|
|
|
|
# the source code of the file changed, we need to reload
|
2008-09-18 16:09:06 +00:00
|
|
|
checksum = pickle.load(f)
|
2008-09-17 14:19:46 +00:00
|
|
|
if self.checksum != checksum:
|
|
|
|
self.reset()
|
|
|
|
return
|
2013-11-21 19:15:57 +00:00
|
|
|
# if marshal_load fails then we need to reload
|
|
|
|
try:
|
2020-01-28 06:05:00 +00:00
|
|
|
self.code = marshal.load(f)
|
2013-11-22 16:18:25 +00:00
|
|
|
except (EOFError, ValueError, TypeError):
|
2013-11-21 19:15:57 +00:00
|
|
|
self.reset()
|
|
|
|
return
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2022-04-10 20:55:22 +00:00
|
|
|
def write_bytecode(self, f: t.IO[bytes]) -> None:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""Dump the bytecode into the file or file like object passed."""
|
2008-09-17 14:19:46 +00:00
|
|
|
if self.code is None:
|
2020-01-10 15:46:18 +00:00
|
|
|
raise TypeError("can't write empty bucket")
|
2008-09-17 14:19:46 +00:00
|
|
|
f.write(bc_magic)
|
2008-09-18 16:09:06 +00:00
|
|
|
pickle.dump(self.checksum, f, 2)
|
2020-01-28 06:05:00 +00:00
|
|
|
marshal.dump(self.code, f)
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def bytecode_from_string(self, string: bytes) -> None:
|
|
|
|
"""Load bytecode from bytes."""
|
2011-05-24 15:50:08 +00:00
|
|
|
self.load_bytecode(BytesIO(string))
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def bytecode_to_string(self) -> bytes:
|
|
|
|
"""Return the bytecode as bytes."""
|
2011-05-24 15:50:08 +00:00
|
|
|
out = BytesIO()
|
2008-09-17 19:28:01 +00:00
|
|
|
self.write_bytecode(out)
|
2008-09-17 14:19:46 +00:00
|
|
|
return out.getvalue()
|
|
|
|
|
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
class BytecodeCache:
|
2008-09-17 14:19:46 +00:00
|
|
|
"""To implement your own bytecode cache you have to subclass this class
|
2008-09-17 19:28:01 +00:00
|
|
|
and override :meth:`load_bytecode` and :meth:`dump_bytecode`. Both of
|
2020-01-27 05:12:52 +00:00
|
|
|
these methods are passed a :class:`~jinja2.bccache.Bucket`.
|
2008-09-17 19:28:01 +00:00
|
|
|
|
|
|
|
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):
|
2010-02-09 14:04:51 +00:00
|
|
|
with open(filename, 'rb') as f:
|
2008-09-17 19:28:01 +00:00
|
|
|
bucket.load_bytecode(f)
|
|
|
|
|
|
|
|
def dump_bytecode(self, bucket):
|
|
|
|
filename = path.join(self.directory, bucket.key)
|
2010-02-09 14:04:51 +00:00
|
|
|
with open(filename, 'wb') as f:
|
2008-09-17 19:28:01 +00:00
|
|
|
bucket.write_bytecode(f)
|
|
|
|
|
|
|
|
A more advanced version of a filesystem based bytecode cache is part of
|
2019-10-23 19:29:46 +00:00
|
|
|
Jinja.
|
2008-09-17 14:19:46 +00:00
|
|
|
"""
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def load_bytecode(self, bucket: Bucket) -> None:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""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.
|
2008-09-17 14:19:46 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def dump_bytecode(self, bucket: Bucket) -> None:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""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.
|
2008-09-17 14:19:46 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def clear(self) -> None:
|
2019-10-23 19:29:46 +00:00
|
|
|
"""Clears the cache. This method is not used by Jinja but should be
|
2008-09-17 19:28:01 +00:00
|
|
|
implemented to allow applications to clear the bytecode cache used
|
|
|
|
by a particular environment.
|
|
|
|
"""
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def get_cache_key(
|
|
|
|
self, name: str, filename: t.Optional[t.Union[str]] = None
|
|
|
|
) -> str:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""Returns the unique hash key for this template name."""
|
2020-01-10 15:46:18 +00:00
|
|
|
hash = sha1(name.encode("utf-8"))
|
2021-05-08 20:03:15 +00:00
|
|
|
|
2008-09-17 19:28:01 +00:00
|
|
|
if filename is not None:
|
2021-08-02 18:54:02 +00:00
|
|
|
hash.update(f"|{filename}".encode())
|
2021-05-08 20:03:15 +00:00
|
|
|
|
2008-09-17 19:28:01 +00:00
|
|
|
return hash.hexdigest()
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def get_source_checksum(self, source: str) -> str:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""Returns a checksum for the source."""
|
2020-01-10 15:46:18 +00:00
|
|
|
return sha1(source.encode("utf-8")).hexdigest()
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def get_bucket(
|
|
|
|
self,
|
|
|
|
environment: "Environment",
|
|
|
|
name: str,
|
|
|
|
filename: t.Optional[str],
|
|
|
|
source: str,
|
|
|
|
) -> Bucket:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""Return a cache bucket for the given template. All arguments are
|
|
|
|
mandatory but filename may be `None`.
|
|
|
|
"""
|
|
|
|
key = self.get_cache_key(name, filename)
|
2008-09-17 14:19:46 +00:00
|
|
|
checksum = self.get_source_checksum(source)
|
2008-09-17 19:28:01 +00:00
|
|
|
bucket = Bucket(environment, key, checksum)
|
|
|
|
self.load_bytecode(bucket)
|
2008-09-17 14:19:46 +00:00
|
|
|
return bucket
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def set_bucket(self, bucket: Bucket) -> None:
|
2008-09-17 19:28:01 +00:00
|
|
|
"""Put the bucket into the cache."""
|
|
|
|
self.dump_bytecode(bucket)
|
|
|
|
|
|
|
|
|
|
|
|
class FileSystemBytecodeCache(BytecodeCache):
|
|
|
|
"""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.
|
|
|
|
|
2014-01-10 10:40:51 +00:00
|
|
|
If no directory is specified a default cache directory is selected. On
|
|
|
|
Windows the user's temp directory is used, on UNIX systems a directory
|
|
|
|
is created for the user in the system temp directory.
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2008-09-17 19:28:01 +00:00
|
|
|
The pattern can be used to have multiple separate caches operate on the
|
2020-01-27 05:12:52 +00:00
|
|
|
same directory. The default pattern is ``'__jinja2_%s.cache'``. ``%s``
|
2008-09-17 19:28:01 +00:00
|
|
|
is replaced with the cache key.
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2008-09-17 19:28:01 +00:00
|
|
|
>>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')
|
2008-09-18 16:09:06 +00:00
|
|
|
|
|
|
|
This bytecode cache supports clearing of the cache using the clear method.
|
2008-09-17 19:28:01 +00:00
|
|
|
"""
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __init__(
|
|
|
|
self, directory: t.Optional[str] = None, pattern: str = "__jinja2_%s.cache"
|
|
|
|
) -> None:
|
2008-09-17 19:28:01 +00:00
|
|
|
if directory is None:
|
2014-01-10 10:40:51 +00:00
|
|
|
directory = self._get_default_cache_dir()
|
2008-09-17 14:19:46 +00:00
|
|
|
self.directory = directory
|
|
|
|
self.pattern = pattern
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def _get_default_cache_dir(self) -> str:
|
2021-05-14 00:24:19 +00:00
|
|
|
def _unsafe_dir() -> "te.NoReturn":
|
2020-01-10 15:46:18 +00:00
|
|
|
raise RuntimeError(
|
|
|
|
"Cannot determine safe temp directory. You "
|
|
|
|
"need to explicitly provide one."
|
|
|
|
)
|
2014-06-06 15:28:09 +00:00
|
|
|
|
2014-01-10 10:40:51 +00:00
|
|
|
tmpdir = tempfile.gettempdir()
|
|
|
|
|
|
|
|
# On windows the temporary directory is used specific unless
|
|
|
|
# explicitly forced otherwise. We can just use that.
|
2020-01-10 15:46:18 +00:00
|
|
|
if os.name == "nt":
|
2014-01-10 10:40:51 +00:00
|
|
|
return tmpdir
|
2020-01-10 15:46:18 +00:00
|
|
|
if not hasattr(os, "getuid"):
|
2014-06-06 15:28:09 +00:00
|
|
|
_unsafe_dir()
|
2014-01-10 10:40:51 +00:00
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
dirname = f"_jinja2-cache-{os.getuid()}"
|
2014-01-10 10:40:51 +00:00
|
|
|
actual_dir = os.path.join(tmpdir, dirname)
|
2014-06-06 15:28:09 +00:00
|
|
|
|
2014-01-10 10:40:51 +00:00
|
|
|
try:
|
2014-06-06 16:44:49 +00:00
|
|
|
os.mkdir(actual_dir, stat.S_IRWXU)
|
2014-01-10 10:40:51 +00:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2014-06-06 15:28:09 +00:00
|
|
|
try:
|
2014-06-06 16:44:49 +00:00
|
|
|
os.chmod(actual_dir, stat.S_IRWXU)
|
|
|
|
actual_dir_stat = os.lstat(actual_dir)
|
2020-01-10 15:46:18 +00:00
|
|
|
if (
|
|
|
|
actual_dir_stat.st_uid != os.getuid()
|
|
|
|
or not stat.S_ISDIR(actual_dir_stat.st_mode)
|
|
|
|
or stat.S_IMODE(actual_dir_stat.st_mode) != stat.S_IRWXU
|
|
|
|
):
|
2014-06-06 15:28:09 +00:00
|
|
|
_unsafe_dir()
|
2014-01-10 10:40:51 +00:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
|
2014-02-09 08:40:59 +00:00
|
|
|
actual_dir_stat = os.lstat(actual_dir)
|
2020-01-10 15:46:18 +00:00
|
|
|
if (
|
|
|
|
actual_dir_stat.st_uid != os.getuid()
|
|
|
|
or not stat.S_ISDIR(actual_dir_stat.st_mode)
|
|
|
|
or stat.S_IMODE(actual_dir_stat.st_mode) != stat.S_IRWXU
|
|
|
|
):
|
2014-06-06 15:28:09 +00:00
|
|
|
_unsafe_dir()
|
2014-02-09 08:40:59 +00:00
|
|
|
|
2014-01-10 10:40:51 +00:00
|
|
|
return actual_dir
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def _get_cache_filename(self, bucket: Bucket) -> str:
|
2020-01-29 04:16:59 +00:00
|
|
|
return os.path.join(self.directory, self.pattern % (bucket.key,))
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def load_bytecode(self, bucket: Bucket) -> None:
|
|
|
|
filename = self._get_cache_filename(bucket)
|
|
|
|
|
2022-04-10 20:55:22 +00:00
|
|
|
# Don't test for existence before opening the file, since the
|
|
|
|
# file could disappear after the test before the open.
|
|
|
|
try:
|
|
|
|
f = open(filename, "rb")
|
|
|
|
except (FileNotFoundError, IsADirectoryError, PermissionError):
|
|
|
|
# PermissionError can occur on Windows when an operation is
|
|
|
|
# in progress, such as calling clear().
|
|
|
|
return
|
|
|
|
|
|
|
|
with f:
|
|
|
|
bucket.load_bytecode(f)
|
2008-09-17 14:19:46 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def dump_bytecode(self, bucket: Bucket) -> None:
|
2022-04-10 20:55:22 +00:00
|
|
|
# Write to a temporary file, then rename to the real name after
|
|
|
|
# writing. This avoids another process reading the file before
|
|
|
|
# it is fully written.
|
|
|
|
name = self._get_cache_filename(bucket)
|
|
|
|
f = tempfile.NamedTemporaryFile(
|
|
|
|
mode="wb",
|
|
|
|
dir=os.path.dirname(name),
|
|
|
|
prefix=os.path.basename(name),
|
|
|
|
suffix=".tmp",
|
|
|
|
delete=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def remove_silent() -> None:
|
|
|
|
try:
|
|
|
|
os.remove(f.name)
|
|
|
|
except OSError:
|
|
|
|
# Another process may have called clear(). On Windows,
|
|
|
|
# another program may be holding the file open.
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
with f:
|
|
|
|
bucket.write_bytecode(f)
|
|
|
|
except BaseException:
|
|
|
|
remove_silent()
|
|
|
|
raise
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.replace(f.name, name)
|
|
|
|
except OSError:
|
|
|
|
# Another process may have called clear(). On Windows,
|
|
|
|
# another program may be holding the file open.
|
|
|
|
remove_silent()
|
|
|
|
except BaseException:
|
|
|
|
remove_silent()
|
|
|
|
raise
|
2008-09-17 19:28:01 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def clear(self) -> None:
|
2008-09-18 14:23:33 +00:00
|
|
|
# imported lazily here because google app-engine doesn't support
|
|
|
|
# write access on the file system and the function does not exist
|
|
|
|
# normally.
|
|
|
|
from os import remove
|
2020-01-10 15:46:18 +00:00
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
files = fnmatch.filter(os.listdir(self.directory), self.pattern % ("*",))
|
2008-09-17 20:25:04 +00:00
|
|
|
for filename in files:
|
2008-09-17 19:28:01 +00:00
|
|
|
try:
|
2020-01-28 06:05:00 +00:00
|
|
|
remove(os.path.join(self.directory, filename))
|
2008-09-17 19:28:01 +00:00
|
|
|
except OSError:
|
|
|
|
pass
|
2008-09-18 16:09:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MemcachedBytecodeCache(BytecodeCache):
|
|
|
|
"""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:
|
|
|
|
|
2019-07-26 19:12:41 +00:00
|
|
|
- `cachelib <https://github.com/pallets/cachelib>`_
|
|
|
|
- `python-memcached <https://pypi.org/project/python-memcached/>`_
|
2008-09-18 16:09:06 +00:00
|
|
|
|
|
|
|
(Unfortunately the django cache interface is not compatible because it
|
2020-01-27 22:12:52 +00:00
|
|
|
does not support storing binary data, only text. You can however pass
|
2008-09-19 09:55:17 +00:00
|
|
|
the underlying cache client to the bytecode cache which is available
|
2008-09-18 16:09:06 +00:00
|
|
|
as `django.core.cache.cache._client`.)
|
|
|
|
|
|
|
|
The minimal interface for the client passed to the constructor is this:
|
|
|
|
|
|
|
|
.. class:: MinimalClientInterface
|
|
|
|
|
|
|
|
.. method:: set(key, value[, timeout])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
.. method:: get(key)
|
|
|
|
|
|
|
|
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.
|
2013-05-19 10:06:18 +00:00
|
|
|
|
2020-01-27 05:12:52 +00:00
|
|
|
.. versionadded:: 2.7
|
|
|
|
Added support for ignoring memcache errors through the
|
|
|
|
`ignore_memcache_errors` parameter.
|
2008-09-18 16:09:06 +00:00
|
|
|
"""
|
|
|
|
|
2020-01-10 15:46:18 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-05-08 20:03:15 +00:00
|
|
|
client: "_MemcachedClient",
|
|
|
|
prefix: str = "jinja2/bytecode/",
|
|
|
|
timeout: t.Optional[int] = None,
|
|
|
|
ignore_memcache_errors: bool = True,
|
2020-01-10 15:46:18 +00:00
|
|
|
):
|
2008-09-18 16:09:06 +00:00
|
|
|
self.client = client
|
|
|
|
self.prefix = prefix
|
|
|
|
self.timeout = timeout
|
2013-05-19 10:06:18 +00:00
|
|
|
self.ignore_memcache_errors = ignore_memcache_errors
|
2008-09-18 16:09:06 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def load_bytecode(self, bucket: Bucket) -> None:
|
2013-02-18 18:26:40 +00:00
|
|
|
try:
|
|
|
|
code = self.client.get(self.prefix + bucket.key)
|
2013-05-19 10:06:18 +00:00
|
|
|
except Exception:
|
|
|
|
if not self.ignore_memcache_errors:
|
|
|
|
raise
|
2021-05-08 20:03:15 +00:00
|
|
|
else:
|
2008-09-18 16:09:06 +00:00
|
|
|
bucket.bytecode_from_string(code)
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def dump_bytecode(self, bucket: Bucket) -> None:
|
|
|
|
key = self.prefix + bucket.key
|
|
|
|
value = bucket.bytecode_to_string()
|
|
|
|
|
2013-02-18 18:26:40 +00:00
|
|
|
try:
|
2021-05-08 20:03:15 +00:00
|
|
|
if self.timeout is not None:
|
|
|
|
self.client.set(key, value, self.timeout)
|
|
|
|
else:
|
|
|
|
self.client.set(key, value)
|
2013-05-19 10:06:18 +00:00
|
|
|
except Exception:
|
|
|
|
if not self.ignore_memcache_errors:
|
|
|
|
raise
|