Redis simple example

不言
Release: 2023-03-25 15:48:01
Original
1087 people have browsed it

This article mainly introduces simple examples of Redis, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

<?php
/**
 * Created by PhpStorm.
 * User: Perilla
 * Date: 2018/4/27
 * Time: 9:13
 */

namespace app\index\controller;

/**
 * Class Reids
 * 作用:学习redis
 * 时间:2018年4月27日15:07:28
 * (基础已迁移到extend/Reids)
 *
 *
 * 队列的rPop是具有原子性的,即使处理并发,也是一个个处理,不会出现重复和超卖的情况
 * Set的特性是不能重复
 * 用户抢购队列(List)
 * 商品队列(List)
 * 订单信息(Hash集合)
 * 购买成功用户(Set集合)
 */
class Reids{
    private $reids;

    /**
     * 初始化一个reids
     */
    public function __construct(\Redis $reids){
        header("content-type:text/html;charset=utf-8");
        $this->reids = $reids;
        $this->reids->connect(&#39;127.0.0.1&#39;,6379);
        if($this->reids->ping() == "+PONG"){
            echo "reids 正常运行<br/>".PHP_EOL;
        }else{
            echo "reids 开启失败<br/>".PHP_EOL;
        }
    }

    /**
     * 创建一个货物库  货物n个
     */
    public function goods($n){
    	$this->reids->delete(&#39;goodsLists&#39;);
        $this->reids->delete(&#39;killsLists&#39;);
        $this->reids->delete(&#39;ordersLists&#39;);
        $this->reids->delete(&#39;usersLists&#39;);
        for($i = 1;$i<=$n;$i++){
            $this->reids->lpush(&#39;goodsLists&#39;,$i);
        }
        echo &#39;进货成功,商品数:&#39;.$this->reids->lsize(&#39;goodsLists&#39;);
    }
    public function getkillsLists(){
        $count = $this->reids->scard(&#39;killsLists&#39;);
        $this->dumps("购买数量",$count);

        $arr = $this->reids->smembers(&#39;killsLists&#39;);
        $this->dumps("购买清单",$arr);


        $length = $this->reids->lsize(&#39;goodsLists&#39;);
        $this->dumps("商品剩余数量:",$length);


        $arr = $this->reids->hgetall(&#39;ordersLists&#39;);
        $this->dumps("商品购买详情:",$arr);

    }

    /**
     * 秒杀过程
     * usersLists  用户表(List)
     * goodsLists  货物表(List)
     * killsLists  秒杀成功表(Set集合)
     * ordersLists 订单表(Hash集合)
     */
    public function kills(){
        //查询库存
        if($this->reids->lLen(&#39;goodsLists&#39;) == 0) {
            $this->dumps("商品已售完...");
            exit;
        }
        //查询是否购买过
        $uid = rand(100000,999999);
        if($this->reids->sIsMember(&#39;killsLists&#39;,$uid)){
            $this->dumps(&#39;你已经购买过了!&#39;);
            exit;
        }
        //购买
        $goods_id = $this->reids->rpop(&#39;goodsLists&#39;);
        if($goods_id){
            $this->reids->sAdd(&#39;killsLists&#39;,$uid);
            $value = array(
                &#39;uid&#39;        =>  $uid,
                &#39;goods_id&#39;   =>  $goods_id,
                &#39;time&#39;       =>  time(),
            );
            $this->reids->hSet(&#39;ordersLists&#39;,$uid,json_encode($value));
            $this->dumps(&#39;购买成功。&#39;);
        }


    }


    /**
     * 按一定规律打印一些数据
     */
    public function dumps($demo,$val = ""){
        echo $demo.PHP_EOL;
        if($val){
            dump($val);
        }
        echo PHP_EOL;
    }
}
Copy after login

Related recommendations:

Common tips for PHP operating Redis

CI framework (CodeIgniter) method of operating redis


The above is the detailed content of Redis simple example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!