Advertisement

PHP+Redis 利用列表 list 实现简单队列

阅读量:

阅读目录

在这里插入图片描述
复制代码
    <?php
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->auth('123456');
    
    
    // 进队列
    // push data to queue
    $userId = mt_rand(000000, 999999);
    $redis->rpush('QUEUE_NAME',json_encode(['user_id' => $userId]));
    $userId = mt_rand(000000, 999999);
    $redis->rpush('QUEUE_NAME',json_encode(['user_id' => $userId]));
    $userId = mt_rand(000000, 999999);
    $redis->rpush('QUEUE_NAME',json_encode(['user_id' => $userId]));
    echo "数据进队列成功 \n";
    echo "push data to queue success \n";
    
    // 查看队列
    // show queue
    $res = $redis->lrange('QUEUE_NAME', 0, 1000);
    echo "当前队列数据为: \n";
    echo "The queue's data are: \n";
    print_r($res);
    
    echo "----------------------------- \n";
    
    // 出队列
    // pop up the earlier data from queue
    $redis->lpop('QUEUE_NAME');
    echo "数据出队列成功 \n";
    echo "pop up success \n";
    
    // 查看队列
    $res = $redis->lrange('QUEUE_NAME', 0, 1000);
    echo "当前队列数据为: \n";
    echo "The queue's data are: \n";
    print_r($res);
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI助手

打印结果:

复制代码
    PS E:\PDF> php .\index.php
    数据进队列成功
    push data to queue success
    当前队列数据为:
    The queue's data are:
    Array
    (
    [0] => {"user_id":882821}
    [1] => {"user_id":594503}
    [2] => {"user_id":864007}
    )
    -----------------------------
    数据出队列成功
    pop up success
    当前队列数据为:
    The queue's data are:
    Array
    (
    [0] => {"user_id":594503}
    [1] => {"user_id":864007}
    )
    PS E:\PDF>
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI助手

全部评论 (0)

还没有任何评论哟~