Ejemplo n.º 1
0
class Cache:
    """Simple cache backend wrapper using Pinterest's pymemcache;

    Attributes:
        client (Client): The client object for a single memcached server;
    """
    def __init__(self):
        self.client = Client((HOST, POST), serde=JsonSerde())

    def __repr__(self):
        return "<Cache {}>".format(self.client.server)

    def get_stats(self, *args):
        """Runs the memcached `stats` command;

        Args:
            *arg (list): extra string arguments to the “stats” command;
        Returns:
            A dictionary of the returned stats;
        """
        return self.client.stats(args)

    def set(self, key, value):
        """Cache setter;

        Args:
            key (str): unique identifier of pair;
            value (str): value for the key;
        """
        self.client.set(key, value)

    def set_multiple(self, value):
        """A convenience function for setting multiple values;

        Args:
            key (str): unique identifier of pair;
            value (str): value for the key;
        """
        self.client.set_multi(value)

    def get(self, key):
        """Cache getter;

        Args:
            key (str): unique identifier of pair;

        Returns:
            The value for the key if was found;

        Raises:
            Exception if the key wasn’t found;
        """
        return self.client.get(key)

    def add(self, key, value):
        """Runs memcached “add” command;

        Store data, only if it does not already exist;

        Args:
            key (str): unique identifier of pair;
            value (str): value for the key;
        Returns:
             return True if value was stored, False if it was not;
        """

        return self.client.add(key, value)

    def replace(self, key, value):
        """Runs memcached “replace” command;

        Store data, but only if the data already exists;

        Args:
            key (str): unique identifier of pair;
            value (str): value for the key;
        Returns:
             return True if value was stored, False if it was not;
        """

        return self.client.replace(key, value)

    def append(self, key, value):
        """Runs memcached append command;


        Args:
            key (str): unique identifier of pair;
            value (str): value for the key;
        Returns:
             return True;
        """

        return self.client.append(key, value)

    def prepend(self, key, value):
        """Runs memcached “prepend” command;

        Same as append, but adding new data before existing data;

        Args:
            key (str): unique identifier of pair;
            value (str): value for the key;
        Returns:
             return True;
        """

        return self.client.prepend(key, value)

    def delete(self, key):
        """Runs memcached delete command;"""
        return self.client.delete(key)

    def delete_many(self, keys):
        """A convenience function to delete multiple keys;"""
        return self.client.delete_many(keys)
Ejemplo n.º 2
0
    for i in range(ITERATION_NUM):
        key, value = f"some_key-{i}", f"some_value-{i}"

        # cmd "set", create/update key:value
        assert client.set(key, value)

        # cmd "get", get value with key
        result = client.get(key).decode("utf-8")
        assert result == value

        # cmd "delete", delete key
        assert client.delete(key)

        # cmd "add", create key:value
        assert client.set(key, value)
        assert client.add(key, value)

        # cmd "append", append value
        res = client.append(key, "_suffix")
        value += "_suffix"
        assert client.get(key).decode("utf-8") == value

        # cmd "preppend", preppend value
        res = client.prepend(key, "prefix_")
        value = "prefix_" + value
        assert client.get(key).decode("utf-8") == value

        # cmd "replace", preppend value
        res = client.replace(key, "empty")
        assert client.get(key).decode("utf-8") == "empty"