How to use Queue in StackExchange.Redis

7.4k views Asked by At

I am confused about how to use Queue in StackExchange.Redis. I have tried download the source code and check the documentation. I still can not find how to use it.

please give me suggestion.

thanks very much.

1

There are 1 answers

0
JonCole On

Redis supports both queues and stacks through the LPUSH, LPOP, RPUSH and RPOP commands. It is just a matter of calling the right operations on a list. Below are sample implementations of a queue and a stack as a reference. "Connection" in the code below is just an instance of a ConnectionMultiplexer

static class RedisStack
{
    public static void Push(RedisKey stackName, RedisValue value)
    {
        Connection.GetDatabase().ListRightPush(stackName, value);
    }

    public static RedisValue Pop(RedisKey stackName)
    {
        return Connection.GetDatabase().ListRightPop(stackName);
    }
}

static class RedisQueue
{
    public static void Push(RedisKey queueName, RedisValue value)
    {
        Connection.GetDatabase().ListRightPush(queueName, value);
    }

    public static RedisValue Pop(RedisKey queueName)
    {
        return Connection.GetDatabase().ListLeftPop(queueName);
    }
}