예제 #1
0
 def __init__(self, use_memcache, cache_size, cache_folder, auto_reload,
              cache_salt=None):
     if use_memcache:
         self.__memcache = CacheDict(cache_size)
     else:
         self.__memcache = None
     self.__cache_folder = cache_folder
     if not hasattr(self, 'check_source_changed'):
         self.__auto_reload = False
     else:
         self.__auto_reload = auto_reload
     self.__salt = cache_salt
     self.__times = {}
     self.__lock = Lock()
예제 #2
0
 def __init__(self, use_memcache, cache_size, cache_folder, auto_reload, cache_salt=None):
     if use_memcache:
         self.__memcache = CacheDict(cache_size)
     else:
         self.__memcache = None
     self.__cache_folder = cache_folder
     if not hasattr(self, "check_source_changed"):
         self.__auto_reload = False
     else:
         self.__auto_reload = auto_reload
     self.__salt = cache_salt
     self.__times = {}
     self.__lock = Lock()
예제 #3
0
class CachedLoaderMixin(object):
    """
    Mixin this class to implement simple memory and disk caching. The
    memcaching just uses a dict in the loader so if you have a global
    environment or at least a global loader this can speed things up.

    If the memcaching is enabled you can use (with Jinja 1.1 onwards)
    the `clear_memcache` function to clear the cache.

    For memcached support check the `MemcachedLoaderMixin`.
    """
    def __init__(self,
                 use_memcache,
                 cache_size,
                 cache_folder,
                 auto_reload,
                 cache_salt=None):
        if use_memcache:
            self.__memcache = CacheDict(cache_size)
        else:
            self.__memcache = None
        self.__cache_folder = cache_folder
        if not hasattr(self, 'check_source_changed'):
            self.__auto_reload = False
        else:
            self.__auto_reload = auto_reload
        self.__salt = cache_salt
        self.__times = {}
        self.__lock = Lock()

    def clear_memcache(self):
        """
        Clears the memcache.
        """
        if self.__memcache is not None:
            self.__memcache.clear()

    def load(self, environment, name, translator):
        """
        Load and translate a template. First we check if there is a
        cached version of this template in the memory cache. If this is
        not the cache check for a compiled template in the disk cache
        folder. And if none of this is the case we translate the temlate,
        cache and return it.
        """
        self.__lock.acquire()
        try:
            # caching is only possible for the python translator. skip
            # all other translators
            if translator is not PythonTranslator:
                return super(CachedLoaderMixin,
                             self).load(environment, name, translator)

            tmpl = None
            save_to_disk = False
            push_to_memory = False

            # auto reload enabled? check for the last change of
            # the template
            if self.__auto_reload:
                last_change = self.check_source_changed(environment, name)
            else:
                last_change = None

            # check if we have something in the memory cache and the
            # memory cache is enabled.
            if self.__memcache is not None:
                if name in self.__memcache:
                    tmpl = self.__memcache[name]
                    # if auto reload is enabled check if the template changed
                    if last_change and last_change > self.__times[name]:
                        tmpl = None
                        push_to_memory = True
                else:
                    push_to_memory = True

            # 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,
                                         self.__salt)
                if last_change is not None:
                    try:
                        cache_time = path.getmtime(cache_fn)
                    except OSError:
                        cache_time = 0
                if last_change is None or (cache_time
                                           and last_change <= cache_time):
                    try:
                        f = file(cache_fn, 'rb')
                    except IOError:
                        tmpl = None
                        save_to_disk = True
                    else:
                        try:
                            tmpl = Template.load(environment, f)
                        finally:
                            f.close()
                else:
                    save_to_disk = True

            # if we still have no template we load, parse and translate it.
            if tmpl is None:
                tmpl = super(CachedLoaderMixin,
                             self).load(environment, name, translator)

            # save the compiled template on the disk if enabled
            if save_to_disk:
                f = file(cache_fn, 'wb')
                try:
                    tmpl.dump(f)
                finally:
                    f.close()

            # if memcaching is enabled and the template not loaded
            # we add that there.
            if push_to_memory:
                self.__times[name] = time.time()
                self.__memcache[name] = tmpl
            return tmpl
        finally:
            self.__lock.release()
예제 #4
0
class CachedLoaderMixin(object):
    """
    Mixin this class to implement simple memory and disk caching. The
    memcaching just uses a dict in the loader so if you have a global
    environment or at least a global loader this can speed things up.

    If the memcaching is enabled you can use (with Jinja 1.1 onwards)
    the `clear_memcache` function to clear the cache.

    For memcached support check the `MemcachedLoaderMixin`.
    """

    def __init__(self, use_memcache, cache_size, cache_folder, auto_reload, cache_salt=None):
        if use_memcache:
            self.__memcache = CacheDict(cache_size)
        else:
            self.__memcache = None
        self.__cache_folder = cache_folder
        if not hasattr(self, "check_source_changed"):
            self.__auto_reload = False
        else:
            self.__auto_reload = auto_reload
        self.__salt = cache_salt
        self.__times = {}
        self.__lock = Lock()

    def clear_memcache(self):
        """
        Clears the memcache.
        """
        if self.__memcache is not None:
            self.__memcache.clear()

    def load(self, environment, name, translator):
        """
        Load and translate a template. First we check if there is a
        cached version of this template in the memory cache. If this is
        not the cache check for a compiled template in the disk cache
        folder. And if none of this is the case we translate the temlate,
        cache and return it.
        """
        self.__lock.acquire()
        try:
            # caching is only possible for the python translator. skip
            # all other translators
            if not issubclass(translator, PythonTranslator):
                return super(CachedLoaderMixin, self).load(environment, name, translator)

            tmpl = None
            save_to_disk = False
            push_to_memory = False

            # auto reload enabled? check for the last change of
            # the template
            if self.__auto_reload:
                last_change = self.check_source_changed(environment, name)
            else:
                last_change = None

            # check if we have something in the memory cache and the
            # memory cache is enabled.
            if self.__memcache is not None:
                if name in self.__memcache:
                    tmpl = self.__memcache[name]
                    # if auto reload is enabled check if the template changed
                    if last_change and last_change > self.__times[name]:
                        tmpl = None
                        push_to_memory = True
                else:
                    push_to_memory = True

            # 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, self.__salt)
                if last_change is not None:
                    try:
                        cache_time = path.getmtime(cache_fn)
                    except OSError:
                        cache_time = 0
                if last_change is None or (cache_time and last_change <= cache_time):
                    try:
                        f = file(cache_fn, "rb")
                    except IOError:
                        tmpl = None
                        save_to_disk = True
                    else:
                        try:
                            tmpl = Template.load(environment, f)
                        finally:
                            f.close()
                else:
                    save_to_disk = True

            # if we still have no template we load, parse and translate it.
            if tmpl is None:
                tmpl = super(CachedLoaderMixin, self).load(environment, name, translator)

            # save the compiled template on the disk if enabled
            if save_to_disk:
                f = file(cache_fn, "wb")
                try:
                    tmpl.dump(f)
                finally:
                    f.close()

            # if memcaching is enabled and the template not loaded
            # we add that there.
            if push_to_memory:
                self.__times[name] = time.time()
                self.__memcache[name] = tmpl
            return tmpl
        finally:
            self.__lock.release()