Redis实现异步队列


Redis实现异步队列


正文

Redis中有一个数据类型:List。 列表(List) 是简单的字符串(String)列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边), 一个列表最多可以包含 2^32 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。 我们可以使用这个特性实现异步队列。

Redis实现异步队列,相关方法有:

// 添加数据
public function lPush($key, ...$value1){}
// 获取并删除数据
public function rPop($key){}

看一下生产者的例子:

$redis = new Redis();
$redis->connect($config['host'], $config['port']);

$redis->lPush($key, json_encode(["task_name" => $task_name, "task_params" = $task_params]));

消费者的例子:

$redis = new Redis();
$redis->connect($config['host'], $config['port']);

$redis->rPop($key);

/** 消费逻辑 **/ 

redis列表函数原型

/**
 * Adds the string values to the head (left) of the list.
 * Creates the list if the key didn't exist.
 * If the key exists and is not a list, FALSE is returned.
 *
 * @param string $key
 * @param string|mixed $value1... Variadic list of values to push in key, if dont used serialized, used string
 *
 * @return int|bool The new length of the list in case of success, FALSE in case of Failure
 *
 * @link https://redis.io/commands/lpush
 * @example
 * <pre>
 * $redis->lPush('l', 'v1', 'v2', 'v3', 'v4')   // int(4)
 * var_dump( $redis->lRange('l', 0, -1) );
 * // Output:
 * // array(4) {
 * //   [0]=> string(2) "v4"
 * //   [1]=> string(2) "v3"
 * //   [2]=> string(2) "v2"
 * //   [3]=> string(2) "v1"
 * // }
 * </pre>
 */
public function lPush($key, ...$value1)
{
}

/**
 * Adds the string values to the tail (right) of the list.
 * Creates the list if the key didn't exist.
 * If the key exists and is not a list, FALSE is returned.
 *
 * @param string $key
 * @param string|mixed $value1... Variadic list of values to push in key, if dont used serialized, used string
 *
 * @return int|bool The new length of the list in case of success, FALSE in case of Failure
 *
 * @link    https://redis.io/commands/rpush
 * @example
 * <pre>
 * $redis->rPush('l', 'v1', 'v2', 'v3', 'v4');    // int(4)
 * var_dump( $redis->lRange('l', 0, -1) );
 * // Output:
 * // array(4) {
 * //   [0]=> string(2) "v1"
 * //   [1]=> string(2) "v2"
 * //   [2]=> string(2) "v3"
 * //   [3]=> string(2) "v4"
 * // }
 * </pre>
 */
public function rPush($key, ...$value1)
{
}

/**
 * Returns and removes the first element of the list.
 *
 * @param   string $key
 *
 * @return  mixed|bool if command executed successfully BOOL FALSE in case of failure (empty list)
 *
 * @link    https://redis.io/commands/lpop
 * @example
 * <pre>
 * $redis->rPush('key1', 'A');
 * $redis->rPush('key1', 'B');
 * $redis->rPush('key1', 'C');  // key1 => [ 'A', 'B', 'C' ]
 * $redis->lPop('key1');        // key1 => [ 'B', 'C' ]
 * </pre>
 */
public function lPop($key)
{
}

/**
 * Returns and removes the last element of the list.
 *
 * @param   string $key
 *
 * @return  mixed|bool if command executed successfully BOOL FALSE in case of failure (empty list)
 *
 * @link    https://redis.io/commands/rpop
 * @example
 * <pre>
 * $redis->rPush('key1', 'A');
 * $redis->rPush('key1', 'B');
 * $redis->rPush('key1', 'C');  // key1 => [ 'A', 'B', 'C' ]
 * $redis->rPop('key1');        // key1 => [ 'A', 'B' ]
 * </pre>
 */
public function rPop($key)
{
}






参考资料

PHP中利用redis实现消息队列处理高并发请求 https://zhuanlan.zhihu.com/p/99554977

Redis总结(四)redis实现异步队列及延迟队列 https://www.jianshu.com/p/b1395000ca71


返回