예제 #1
0
    def __init__(
            self,
            path,
            expire=None,
            retry_errno=None,
            retries=3,
            **lock_settings):

        self.expire = maybe_integer(expire)
        self.path = make_dir(path)
        self.retries = maybe_integer(retries) or 3
        self.retry_errno = maybe_set(retry_errno)
        self.retry_errno.update(DEFAULT_RETRY_ERRNO)

        # Lock settings
        settings = {}
        for key, value in list(lock_settings.items()):
            if key.startswith('lock_'):
                settings[key.split('lock_', 1)[1]] = value

        lock_path = settings.pop('path', None) or join_paths(self.path, 'locks')
        self.lockme = LockMe(lock_path, **settings)
예제 #2
0
class SaveMe(_SaveMe):
    def __init__(
            self,
            path,
            expire=None,
            retry_errno=None,
            retries=3,
            **lock_settings):

        self.expire = maybe_integer(expire)
        self.path = make_dir(path)
        self.retries = maybe_integer(retries) or 3
        self.retry_errno = maybe_set(retry_errno)
        self.retry_errno.update(DEFAULT_RETRY_ERRNO)

        # Lock settings
        settings = {}
        for key, value in list(lock_settings.items()):
            if key.startswith('lock_'):
                settings[key.split('lock_', 1)[1]] = value

        lock_path = settings.pop('path', None) or join_paths(self.path, 'locks')
        self.lockme = LockMe(lock_path, **settings)

    def lock(self, *args, **kwargs):
        return self.lockme.lock(*args, **kwargs)

    def unlock(self, *args, **kwargs):
        return self.lockme.unlock(*args, **kwargs)

    @lru_cache(1000)
    def get_file_path(self, name):
        name_256 = make_sha256(name)
        return join_paths(self.path, name_256[0], name_256)

    def _contains(self, path, expire=MARKER):
        if expire is MARKER:
            expire = self.expire
        if expire:
            modified_time = file_modified_time(path)
            if not modified_time:
                return False

            if (modified_time + self.expire) < NOW_TIME():
                self._delete_path(path)
                return False

        return isfile(path)

    def __contains__(self, name):
        return self._contains(self.get_file_path(name))

    def _get_binary(self, path, expire=MARKER):
        if expire is MARKER:
            expire = self.expire
        if expire:
            modified_time = file_modified_time(path)
            if not modified_time:
                return None
            elif (modified_time + expire) < NOW_TIME():
                self._delete_path(path)
                return None

        return get_file_binary(path, mode='rb', retries=self.retries, retry_errno=self.retry_errno)

    def get_binary(self, name, expire=MARKER):
        binary = self._get_binary(self.get_file_path(name), expire)
        if binary is None:
            raise KeyError('Missing cache key "%s"' % name)
        else:
            return binary

    def put_binary(self, name, binary, mode='put', expire=MARKER):
        mode = 'ab' if mode == 'append' else 'wb'
        put_binary_on_file(self.get_file_path(name), binary, mode, retries=self.retries, retry_errno=self.retry_errno)

    def _delete_path(self, path):
        remove_file_quietly(path, retries=self.retries, retry_errno=self.retry_errno)

    def __delitem__(self, name):
        file_path = self.get_file_path(name)
        self._delete_path(file_path)