RedisPool.php 3.5 KB
<?php

/**
 * redis 链接池
 * @author:dc
 * @time 2023/2/10 17:04
 * Class RedisPool
 */
class RedisPool {

    /**
     * @var RedisPool[]
     */
    static $instance = [];

    /**
     * @var \Redis
     */
    private $client;


    public function __construct()
    {
        $this->client = new \Redis();

        $this->client->connect(REDIS_HOST,REDIS_PORT);
        // 密码
        REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD);
        // 用库4
        $this->client->select(REDIS_DB);

    }



    /**
     * @param $key
     * @return bool|int
     * @author:dc
     * @time 2023/2/10 18:06
     */
    public function has($key)
    {
        return $this->client->exists($key);
    }


    /**
     * @param $key
     * @param null $default
     * @return mixed|null
     * @author:dc
     * @time 2023/2/10 18:04
     */
    public function get($key, $default=null)
    {
        $data = $this->client->get($key);
        if($data === null){
            return $default;
        }
        return $this->unserialize($data);
    }

    /**
     * @param $key
     * @param $val
     * @param null $ttl
     * @return bool
     * @author:dc
     * @time 2023/2/10 18:02
     */
    public function set($key,$val,$ttl=null) {
        return $this->client->set($key,$this->serialize($val),$ttl);
    }


    /**
     * 如果有key返回false,没有则新增
     * @param $key
     * @param $val
     * @param null $ttl
     * @return mixed
     * @author:dc
     * @time 2023/2/10 17:53
     */
    public function add($key,$val,$ttl=null):mixed {
        return $this->client->eval(
            "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])",
            [$key, $this->serialize($val), $ttl],
            1
        );
    }

    /**
     * @param $key
     * @param $value
     * @return false|int
     * @author:dc
     * @time 2023/2/13 9:07
     */
    public function lPush($key,$value){
        return $this->client->lPush($key,$this->serialize($value));
    }

    /**
     * @param $key
     * @param $value
     * @return false|int
     * @author:dc
     * @time 2023/2/13 9:07
     */
    public function rPush($key,$value){
        return $this->client->rPush($key,$this->serialize($value));
    }

    /**
     * @param $key
     * @return bool|mixed
     * @author:dc
     * @time 2023/2/13 9:08
     */
    public function lPop($key){
        return $this->unserialize($this->client->lPop($key));
    }

    /**
     * @param $key
     * @return mixed|string
     * @author:dc
     * @time 2023/2/13 9:09
     */
    public function rPop($key){
        return $this->unserialize($this->client->rPop($key));
    }



    /**
     * @param $val
     * @return string
     * @author:dc
     * @time 2023/2/10 17:57
     */
    private function serialize($val){
        return $val ? serialize($val) : '';
    }

    /**
     * @param $val
     * @return mixed
     * @author:dc
     * @time 2023/2/10 17:58
     */
    private function unserialize($val){
        return $val ? unserialize($val) : '';
    }




    public function __destruct()
    {
        // TODO: Implement __destruct() method.
        $this->client->close();
        $this->client = null;
    }


    /**
     * @param $cid
     * @return RedisPool
     * @author:dc
     * @time 2023/2/13 9:38
     */
    public static function instance($cid){
        if(empty(static::$instance[$cid])){
            static::$instance[$cid] = new \RedisPool();
        }
        return static::$instance[$cid];
    }










}