• 【redis 封装】


    <?php
    /**
     * Created by PhpStorm.
     * User: andy
     * Date: 18/3/26
     */
    namespace appcommonlib
    edis;
    class Predis {
        public $redis = "";
        /**
         * 定义单例模式的变量
         * @var null
         */
        private static $_instance = null;
    
        public static function getInstance() {
            if(empty(self::$_instance)) {
                self::$_instance = new self();
            }
            return self::$_instance;
        }
    
        private function __construct() {
            $this->redis = new Redis();
            $result = $this->redis->connect(config('redis.host'), config('redis.port'), config('redis.timeOut'));
            if($result === false) {
                throw new Exception('redis connect error');
            }
        }
    
        /**
         * set
         * @param $key
         * @param $value
         * @param int $time
         * @return bool|string
         */
        public function set($key, $value, $time = 0 ) {
            if(!$key) {
                return '';
            }
            if(is_array($value)) {
                $value = json_encode($value);
            }
            if(!$time) {
                return $this->redis->set($key, $value);
            }
    
            return $this->redis->setex($key, $time, $value);
        }
    
        /**
         * get
         * @param $key
         * @return bool|string
         */
        public function get($key) {
            if(!$key) {
                return '';
            }
    
            return $this->redis->get($key);
        }
    
        /**
         * @param $key
         * @return array
         */
        public function sMembers($key) {
            return $this->redis->sMembers($key);
        }
    
        /**
         * @param $name
         * @param $arguments
         * @return array
         */
        public function __call($name, $arguments) {
            //echo $name.PHP_EOL;
            //print_r($arguments);
            if(count($arguments) != 2) {
                return '';
            }
            $this->redis->$name($arguments[0], $arguments[1]);
        }
    }

    总结:采用单例模式:减少资源不必要开销

  • 相关阅读:
    Kruskal算法
    拓扑排序
    邻接表有向图
    邻接矩阵的有向图
    邻接表无向图
    邻接矩阵无向图
    斐波那契堆
    二项堆
    斜堆(待补充)
    项目中maven依赖无法自动下载
  • 原文地址:https://www.cnblogs.com/fyandy/p/10061329.html
Copyright © 2020-2023  润新知