redis实现队列操作
2016-12-27 00:00
169 查看
e:/www/task.php
e:/www/index.php
e:/www/exec.php
运行结果为
<?php class Task{ //一个成员 protected $redis; /** * 初始化 */ public function __construct(){ $this->redis = new \Redis(); $this->redis->connect('127.0.0.1' , '6379'); } /** * 创建任务 * @return [type] [description] */ public function createTask($data = array()){ //处理数据 //加入队列,注意task是一个唯一的键,value可以为json数据, //lpush是redis 的列表 $this->redis->lpush('task' , json_encode($data)); //如果有需要可以设置key过期时间,过期时间为30秒 //$this->redis->expire('user' , 30); } /** * 执行任务 * @return [type] [description] */ public function execTask(){ //处理队列 //统计长度 $length = $this->redis->llen('task'); for($i = 0 ; $i < $length ; $i ++){ //取出最后一个 $item = $this->redis->rpop('task'); //处理一系列的逻辑 var_dump($item); //可以选择睡眠一秒 //sleep(1); } } }
e:/www/index.php
<?php require_once 'task.php'; $task = new \Task(); $task->createTask(array('uid' => 1 , 'content' => '谢谢你的花')); $task->createTask(array('uid' => 2 , 'content' => '我来看看'));
e:/www/exec.php
<?php require_once 'task.php'; $task = new \Task(); $task->execTask();
运行结果为
string(52) "{"uid":1,"content":"\u8c22\u8c22\u4f60\u7684\u82b1"}" string(46) "{"uid":2,"content":"\u6211\u6765\u770b\u770b"}"
相关文章推荐
- Redis与KV存储(RocksDB)融合之编码方式
- Spring集成redis(pipeline方式)
- redis dict字典的源码分析
- Redis的使用场景
- 怎样在Redis通过StackExchange.Redis 存储集合类型List
- redis 在32位系统安装以及使用
- 用Redis Desktop Manager连接Redis
- window下redis的安装及使用
- redis -- 信号量
- Centos7 下安装redis 3.2.6
- redis 点滴(七)sentinel 哨兵
- Centos 6下通过rpm安装redis3.0
- redis 持久化
- redis 启动服务端和客户端
- redis 持久
- redis 与 zookeeper 实现分布式锁的异同
- redis类型讲解[string, list, set, sorted set, hash]
- centos linux 下PHP安装phpredis扩展
- Java连接虚拟机的redis报错问题解决办法
- 基于redis 实现分布式锁的方案