Esempio n. 1
0
class SaveMeMemcached(_SaveMe):
    def __init__(
            self,
            url,
            expire=None,
            **settings):

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

        self.memcache_module = _import_module('memcache')
        self.memcache = self.memcache_module.Client(url.split(';'), **settings)
        self.expire = maybe_integer(expire)
        self.lockme = LockMeMemcached(url, **lock_settings)

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

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

    def format_name(self, name):
        return to_bytes(make_sha256(name))

    def __contains__(self, name):
        return self.memcache.get(self.format_name(name)) is not None

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

    def put_binary(self, name, binary, mode='wb', expire=MARKER):
        name_256 = self.format_name(name)
        if expire is MARKER:
            expire = self.expire

        # Append to existing file
        if mode == 'append' and name in self:
            self.memcache.append(name_256, binary, time=expire or 0)
        else:
            self.memcache.set(name_256, binary, time=expire or 0)

    def __delitem__(self, name):
        self.memcache.delete(self.format_name(name))
Esempio n. 2
0
    def __init__(
            self,
            url,
            expire=None,
            **settings):

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

        self.memcache_module = _import_module('memcache')
        self.memcache = self.memcache_module.Client(url.split(';'), **settings)
        self.expire = maybe_integer(expire)
        self.lockme = LockMeMemcached(url, **lock_settings)