Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Queue in StackExchange.Redis

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.

like image 754
Kevin Shen Avatar asked Jun 18 '15 03:06

Kevin Shen


1 Answers

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);
    }
}
like image 171
JonCole Avatar answered Oct 26 '22 14:10

JonCole