Пример #1
0
def test_get_many_all_found():
    client = Client(None)
    client.sock = MockSocket(
        ['VALUE key1 0 6\r\nvalue1\r\n'
         'VALUE key2 0 6\r\nvalue2\r\nEND\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equal(result, {'key1': 'value1', 'key2': 'value2'})
Пример #2
0
def test_get_set(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.get('key')
    assert result is None

    client.set(b'key', b'value', noreply=False)
    result = client.get(b'key')
    assert result == b'value'

    client.set(b'key2', b'value2', noreply=True)
    result = client.get(b'key2')
    assert result == b'value2'

    result = client.get_many([b'key', b'key2'])
    assert result == {b'key': b'value', b'key2': b'value2'}

    result = client.get_many([])
    assert result == {}
Пример #3
0
def get_set_test(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.get('key')
    tools.assert_equal(result, None)

    client.set(b'key', b'value', noreply=False)
    result = client.get(b'key')
    tools.assert_equal(result, b'value')

    client.set(b'key2', b'value2', noreply=True)
    result = client.get(b'key2')
    tools.assert_equal(result, b'value2')

    result = client.get_many([b'key', b'key2'])
    tools.assert_equal(result, {b'key': b'value', b'key2': b'value2'})

    result = client.get_many([])
    tools.assert_equal(result, {})
Пример #4
0
def get_set_test(host, port):
    client = Client((host, port))
    client.flush_all()

    result = client.get('key')
    tools.assert_equal(result, None)

    client.set('key', 'value', noreply=False)
    result = client.get('key')
    tools.assert_equal(result, 'value')

    client.set('key2', 'value2', noreply=True)
    result = client.get('key2')
    tools.assert_equal(result, 'value2')

    result = client.get_many(['key', 'key2'])
    tools.assert_equal(result, {'key': 'value', 'key2': 'value2'})

    result = client.get_many([])
    tools.assert_equal(result, {})
def get_set_test(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.get('key')
    tools.assert_equal(result, None)

    client.set(b'key', b'value', noreply=False)
    result = client.get(b'key')
    tools.assert_equal(result, b'value')

    client.set(b'key2', b'value2', noreply=True)
    result = client.get(b'key2')
    tools.assert_equal(result, b'value2')

    result = client.get_many([b'key', b'key2'])
    tools.assert_equal(result, {b'key': b'value', b'key2': b'value2'})

    result = client.get_many([])
    tools.assert_equal(result, {})
Пример #6
0
def get_set_test(host, port):
    client = Client((host, port))
    client.flush_all()

    result = client.get('key')
    tools.assert_equal(result, None)

    client.set('key', 'value', noreply=False)
    result = client.get('key')
    tools.assert_equal(result, 'value')

    client.set('key2', 'value2', noreply=True)
    result = client.get('key2')
    tools.assert_equal(result, 'value2')

    result = client.get_many(['key', 'key2'])
    tools.assert_equal(result, {'key': 'value', 'key2': 'value2'})

    result = client.get_many([])
    tools.assert_equal(result, {})
Пример #7
0
	class PyMemcachedCache(BaseCache):
		"""A cache client based on pymemcache. implemented by pure python and support noreply.
		"""
		def __init__(self, config):
			BaseCache.__init__(self, config)
			self._client = PyMemcachedClient((config['host'], config['port']),
				serializer=python_memcache_serializer, deserializer=python_memcache_deserializer,
				connect_timeout=_DEFAULT_SOCKET_TIMEOUT, timeout=_DEFAULT_SOCKET_TIMEOUT,
				key_prefix=config.get('key_prefix', ''))

		def get(self, key):
			return self._client.get(key)

		def delete(self, key, noreply=False):
			self._client.delete(key, noreply)
			return True

		def get_many(self, keys):
			return self._client.get_many(keys)

		def set(self, key, value, timeout=None, noreply=False):
			if timeout is None:
				timeout = self.default_timeout
			return self._client.set(key, value, timeout, noreply)

		def add(self, key, value, timeout=None, noreply=False):
			if timeout is None:
				timeout = self.default_timeout
			return self._client.add(key, value, timeout, noreply)

		def set_many(self, data, timeout=None, noreply=False):
			if timeout is None:
				timeout = self.default_timeout
			return self._client.set_many(data, timeout, noreply)

		def delete_many(self, keys, noreply=False):
			return self._client.delete_many(keys, noreply)

		def clear(self):
			return self._client.flush_all(noreply=False)

		def incr(self, key, delta=1, noreply=False):
			return self._client.incr(key, delta, noreply)

		def decr(self, key, delta=1, noreply=False):
			return self._client.decr(key, delta, noreply)
Пример #8
0
    class PyMemcachedCache(BaseCache):
        """A cache client based on pymemcache. implemented by pure python and support noreply.
        """
        def __init__(self, config):
            BaseCache.__init__(self, config)
            self._client = PyMemcachedClient((config['host'], config['port']),
                serializer=python_memcache_serializer, deserializer=python_memcache_deserializer,
                connect_timeout=_DEFAULT_SOCKET_TIMEOUT, timeout=_DEFAULT_SOCKET_TIMEOUT,
                key_prefix=config.get('key_prefix', ''))

        def get(self, key):
            return self._client.get(key)

        def delete(self, key, noreply=False):
            self._client.delete(key, noreply)
            return True

        def get_many(self, keys):
            return self._client.get_many(keys)

        def set(self, key, value, timeout=None, noreply=False):
            if timeout is None:
                timeout = self.default_timeout
            return self._client.set(key, value, timeout, noreply)

        def add(self, key, value, timeout=None, noreply=False):
            if timeout is None:
                timeout = self.default_timeout
            return self._client.add(key, value, timeout, noreply)

        def set_many(self, data, timeout=None, noreply=False):
            if timeout is None:
                timeout = self.default_timeout
            return self._client.set_many(data, timeout, noreply)

        def delete_many(self, keys, noreply=False):
            return self._client.delete_many(keys, noreply)

        def clear(self):
            return self._client.flush_all(noreply=False)

        def incr(self, key, delta=1, noreply=False):
            return self._client.incr(key, delta, noreply)

        def decr(self, key, delta=1, noreply=False):
            return self._client.decr(key, delta, noreply)
Пример #9
0
def test_cr_nl_boundaries():
    client = Client(None)
    client.sock = MockSocket(['VALUE key1 0 6\r',
                              '\nvalue1\r\n'
                              'VALUE key2 0 6\r\n',
                              'value2\r\n'
                              'END\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket(['VALUE key1 0 6\r\n',
                              'value1\r',
                              '\nVALUE key2 0 6\r\n',
                              'value2\r\n',
                              'END\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket(['VALUE key1 0 6\r\n',
                              'value1\r\n',
                              'VALUE key2 0 6\r',
                              '\nvalue2\r\n',
                              'END\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})


    client.sock = MockSocket(['VALUE key1 0 6\r\n',
                              'value1\r\n',
                              'VALUE key2 0 6\r\n',
                              'value2\r',
                              '\nEND\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket(['VALUE key1 0 6\r\n',
                              'value1\r\n',
                              'VALUE key2 0 6\r\n',
                              'value2\r\n',
                              'END\r',
                              '\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket(['VALUE key1 0 6\r',
                              '\nvalue1\r',
                              '\nVALUE key2 0 6\r',
                              '\nvalue2\r',
                              '\nEND\r',
                              '\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})
Пример #10
0
def test_cr_nl_boundaries():
    client = Client(None)
    client.sock = MockSocket([
        'VALUE key1 0 6\r', '\nvalue1\r\n'
        'VALUE key2 0 6\r\n', 'value2\r\n'
        'END\r\n'
    ])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket([
        'VALUE key1 0 6\r\n', 'value1\r', '\nVALUE key2 0 6\r\n', 'value2\r\n',
        'END\r\n'
    ])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket([
        'VALUE key1 0 6\r\n', 'value1\r\n', 'VALUE key2 0 6\r', '\nvalue2\r\n',
        'END\r\n'
    ])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket([
        'VALUE key1 0 6\r\n', 'value1\r\n', 'VALUE key2 0 6\r\n', 'value2\r',
        '\nEND\r\n'
    ])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket([
        'VALUE key1 0 6\r\n', 'value1\r\n', 'VALUE key2 0 6\r\n', 'value2\r\n',
        'END\r', '\n'
    ])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})

    client.sock = MockSocket([
        'VALUE key1 0 6\r', '\nvalue1\r', '\nVALUE key2 0 6\r', '\nvalue2\r',
        '\nEND\r', '\n'
    ])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equals(result, {'key1': 'value1', 'key2': 'value2'})
Пример #11
0
def test_get_many_none_found():
    client = Client(None)
    client.sock = MockSocket(['END\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equal(result, {})
Пример #12
0
class PyMemcached(Base):
    """A cache client based on pymemcache. Implemented by pure python and support noreply
    """
    def __init__(self, host, port, timeout=None, prefix=''):
        Base.__init__(self, timeout)
        self._client = Client(
            (host, port),
            serializer=python_memcache_serializer,
            deserializer=python_memcache_deserializer,
            connect_timeout=_DEFAULT_SOCKET_TIMEOUT,
            timeout=timeout or _DEFAULT_TIMEOUT,
            key_prefix=prefix,
        )

    def get(self, key):
        """Look up the `key` in cache and return the value of it.

        :param key: the `key` to be looked up
        :returns: the value if it exists and is readable, else ``None``.

        TODO: support __get__
        """
        return self._client.get(_to_native(key))

    def delete(self, key, noreply=False):
        """Delete `key` from cache.

        :param key: the `key` to delete.
        :param noreply: instruct the server to not reply.
        :returns: whether the key been deleted.
        :rtype: boolean

        TODO: __del__
        """
        self._client.delete(_to_native(key), noreply)
        return True

    def get_values(self, *keys):
        """Get valeus by keys

        foo, bar = cache.get_values('foo', 'bar')

        Share same error handling with :meth:`get`
        :param keys: the function acception multiple keys as positional arguments
        """
        return self._client.get_many([_to_native(key) for key in keys])

    def set(self, key, value, timeout=None, noreply=False):
        """Add a new key/value to the cache (overwrite value, if key exists)

        :param key: the key to set
        :param value: the value of the key
        :param timeout: the cache timeout for the key.
                            If not specificed, use the default timeout.
                            If specified 0, key will never expire
        :param noreply: instructs the server to not reply
        :returns: Whether the key existed and has been set
        :rtype: boolean
        TODO: __set__
        """
        if timeout is None:
            timeout = self.timeout
        return self._client.set(_to_native(key), value, timeout, noreply)

    def set_not_overwrite(self, key, value, timeout=None, noreply=False):
        """Works like :meth:`set` but does not overwrite the existing value

        :param key: the key to set
        :param value: the value of the key
        :param timeout: the cache timeout for the key.
                            If not specificed, use the default timeout.
                            If specified 0, key will never expire
        :param noreply: instructs the server to not reply
        :returns: Whether the key existed and has been set
        :rtype: boolean
        """
        if timeout is None:
            timeout = self.timeout
        return self._client.add(_to_native(key), value, timeout, noreply)

    def set_many(self, timeout=None, noreply=False, **kw):
        """Sets multiple key-value pair

        :param timeout: the cache timeout for the key.
                            If not specificed, use the default timeout.
                            If specified 0, key will never expire
        :param noreply: instructs the server to not reply
        :returns: Whether all key-value pairs have been set
        :rtype: boolean
        """
        if timeout is None:
            timeout = self.timeout
        res = self._client.set_many(
            dict((_to_native(k), v) for k, v in kw.items()), timeout, noreply)
        if isinstance(res, list):
            # pymemcache 2.x+ returns a list of keys that failed to update, instead of hard-coded boolean
            return not res
        return res

    def delete_many(self, noreply=False, *keys):
        """Delete multiple keys at once.

        :param keys: The function accept multiple keys as positional arguments
        :param noreply: instructs the server not reply
        :returns: Whether all given keys have been deleted
        :rtype: boolen
        """
        return self._client.delete_many([_to_native(key) for key in keys],
                                        noreply)

    def incr(self, key, delta=1, noreply=False):
        """Increments the value of a key by `delta`. If the key does not yet exists it is initialized with `delta`

        For supporting caches this is an atomic operation

        :param key: the key to increment
        :param delta: the delta to add
        :param noreply: instructs the server not reply
        :returns: The new value or ``None`` for backend errors.
        """
        return self._client.incr(_to_native(key), delta, noreply)

    def decr(self, key, delta=1, noreply=False):
        """Decrements the value of a key by `delta`. If the key does not yet exists it is initialized with `-delta`.

        For supporting caches this is an atomic operation.

        :param key: the key to increment
        :param delta: the delta to subtruct
        :param noreply: instructs the server not reply
        :returns: The new value or `None` for backend errors.
        """
        return self._client.decr(_to_native(key), delta, noreply)

    def clear(self):
        """Clears the cache. Not all caches support completely clearing the cache

        :returns: Whether the cache been cleared.
        :rtype: boolean
        """
        return self._client.flush_all(noreply=False)

    def expire(self, key, timeout):
        """Set a timeout on key. After the timeout has expired, the key will automatically be deleted.

        :param key: key to set timeout
        :param timeout: timeout value in seconds
        :returns: True if timeout was set, False if key does not exist or timeout could not be set
        :rtype: boolean
        """
        # This requires the version of memcached server >= 1.4.8
        return self._client.touch(_to_native(key), timeout)
Пример #13
0
def test_get_many_all_found():
    client = Client(None)
    client.sock = MockSocket(['VALUE key1 0 6\r\nvalue1\r\n'
                              'VALUE key2 0 6\r\nvalue2\r\nEND\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equal(result, {'key1': 'value1', 'key2': 'value2'})
Пример #14
0
def test_get_many_none_found():
    client = Client(None)
    client.sock = MockSocket(['END\r\n'])
    result = client.get_many(['key1', 'key2'])
    tools.assert_equal(result, {})