예제 #1
0
파일: cache.py 프로젝트: machawk1/timegate
    def _check_size(self, key, delete=True):
        """Check the size that a specific TimeMap value is using on disk.

        It deletes if it is more than the maximum size.

        :param key: The TimeMap original resource.
        :param delete: (Optional) When true, the value is deleted.
        Else only a warning is raised.
        :return: The size of the value on disk (0 if it was deleted).
        """
        try:
            fname = md5(key).hexdigest()  # werkzeug key
            fpath = self.path + '/' + fname
            size = os.path.getsize(fpath)
            if size > self.max_file_size and delete:
                message = ("Cache value too big (%dB, max %dB) "
                           "for the TimeMap of %s")
                if delete:
                    message += ". Deleting cached value."
                    os.remove(fpath)
                    size = 0
                logging.warning(message % (size, self.max_file_size, key))
            return size
        except Exception as e:
            logging.error(
                "Exception checking cache value size for TimeMap of %s "
                "Exception: %s" % (key, e))
            return 0
예제 #2
0
    def _check_size(self, key, delete=True):
        """Check the size that a specific TimeMap value is using on disk.

        It deletes if it is more than the maximum size.

        :param key: The TimeMap original resource.
        :param delete: (Optional) When true, the value is deleted.
        Else only a warning is raised.
        :return: The size of the value on disk (0 if it was deleted).
        """
        try:
            fname = md5(key).hexdigest()  # werkzeug key
            fpath = self.path + '/' + fname
            size = os.path.getsize(fpath)
            if size > self.max_file_size and delete:
                message = ('Cache value too big (%dB, max %dB) '
                           'for the TimeMap of %s')
                if delete:
                    message += '. Deleting cached value.'
                    os.remove(fpath)
                    size = 0
                logging.warning(message % (size, self.max_file_size, key))
            return size
        except Exception as e:
            logging.error(
                'Exception checking cache value size for TimeMap of %s '
                'Exception: %s' % (key, e))
            return 0
예제 #3
0
    def __init__(self,
                 path,
                 tolerance,
                 expiration,
                 max_values,
                 run_tests=True,
                 max_file_size=0):
        """Constructor method.

        :param path: The path of the cache database file.
        :param tolerance: The tolerance, in seconds to which a TimeMap is
        considered young enough to be used as is.
        :param expiration: How long, in seconds, the cache entries are stored
        every get will be a CACHE MISS.
        :param max_values: The maximum number of TimeMaps stored in cache
        before some are deleted
        :param run_tests: (Optional) Tests the cache at initialization.
        :param max_file_size: (Optional) The maximum size (in Bytes) for a
        TimeMap cache value. When max_file_size=0, there is no limit to
        a cache value. When max_file_size=X > 0, the cache will not
        store TimeMap that require more than X Bytes on disk.
        """
        # Parameters Check
        if tolerance <= 0 or expiration <= 0 or max_values <= 0:
            raise CacheError('Cannot create cache: all parameters must be > 0')

        self.tolerance = relativedelta(seconds=tolerance)
        self.path = path.rstrip('/')
        self.max_file_size = max(max_file_size, 0)
        self.CHECK_SIZE = self.max_file_size > 0
        self.max_values = max_values
        self.backend = FileSystemCache(path,
                                       threshold=self.max_values,
                                       default_timeout=expiration)

        # Testing cache
        if run_tests:
            try:
                key = b'1'
                val = 1
                self.backend.set(key, val)
                assert (not self.CHECK_SIZE) or self._check_size(key) > 0
                assert self.backend.get(key) == val
                os.remove(os.path.join(self.path, md5(key).hexdigest()))
            except Exception as e:
                raise CacheError('Error testing cache: %s' % e)

        logging.debug('Cache created. max_files = %d. Expiration = %d. '
                      'max_file_size = %d' %
                      (self.max_values, expiration, self.max_file_size))
예제 #4
0
파일: cache.py 프로젝트: machawk1/timegate
    def __init__(self, path, tolerance, expiration, max_values, run_tests=True,
                 max_file_size=0):
        """Constructor method.

        :param path: The path of the cache database file.
        :param tolerance: The tolerance, in seconds to which a TimeMap is
        considered young enough to be used as is.
        :param expiration: How long, in seconds, the cache entries are stored
        every get will be a CACHE MISS.
        :param max_values: The maximum number of TimeMaps stored in cache
        before some are deleted
        :param run_tests: (Optional) Tests the cache at initialization.
        :param max_file_size: (Optional) The maximum size (in Bytes) for a
        TimeMap cache value. When max_file_size=0, there is no limit to
        a cache value. When max_file_size=X > 0, the cache will not
        store TimeMap that require more than X Bytes on disk.
        """
        # Parameters Check
        if tolerance <= 0 or expiration <= 0 or max_values <= 0:
            raise CacheError("Cannot create cache: all parameters must be > 0")

        self.tolerance = relativedelta(seconds=tolerance)
        self.path = path.rstrip('/')
        self.max_file_size = max(max_file_size, 0)
        self.CHECK_SIZE = self.max_file_size > 0
        self.max_values = max_values
        self.backend = FileSystemCache(path,
                                       threshold=self.max_values,
                                       default_timeout=expiration)

        # Testing cache
        if run_tests:
            try:
                key = '1'
                val = 1
                self.backend.set(key, val)
                assert (not self.CHECK_SIZE) or self._check_size(key) > 0
                assert self.backend.get(key) == val
                os.remove(self.path + '/' + md5(key).hexdigest())
            except Exception as e:
                raise CacheError("Error testing cache: %s" % e)

        logging.debug(
            "Cache created. max_files = %d. Expiration = %d. "
            "max_file_size = %d" % (
                self.max_values, expiration, self.max_file_size))