zoukankan      html  css  js  c++  java
  • Redis基础知识之——自定义封装单实例和普通类Redis

    一、普通Redis实例化类:

    class MyRedis
    {
        private $redis;
    
        public function __construct($host = '121.41.88.209', $port = 63789)
        {
            $this->redis = new Redis();
            $this->redis->connect($host, $port);
        }
    
        public function expire($key = null, $time = 0)
        {
            return $this->redis->expire($key, $time);
        }
    
        public function psubscribe($patterns = array(), $callback)
        {
            $this->redis->psubscribe($patterns, $callback);
        }
    
        public function setOption()
        {
            $this->redis->setOption(Redis::OPT_READ_TIMEOUT,-1);
        }
    
    }

    二、单例模式Redis实例化类:

    <?php
    /**
     * Created by PhpStorm.
     * User: Tinywan
     * Date: 2016/7/3
     * Time: 9:26
     * Mail: Overcome.wan@Gmail.com
     * Singleton instance
     */
    
    namespace OrgUtil;
    
    class RedisInstance
    {
        /**
         * 类对象实例数组,共有静态变量
         * @var null
         */
        private static $_instance;
    
        /**
         * 数据库连接资源句柄
         * @var
         */
        private static $_connectSource;
    
        /**
         * 私有化构造函数,防止类外实例化
         * RedisConnect constructor.
         */
        private function __construct()
        {
    
        }
    
        /**
         *  单例方法,用于访问实例的公共的静态方法
         * @return Redis
         * @static
         */
        public static function getInstance()
        {
            if (!(static::$_instance instanceof Redis)) {
                static::$_instance = new Redis();
                self::getInstance()->connect(C('MASTER.HOST'), C('MASTER.PORT'), C('MASTER.TIMEOUT'));
            }
            return static::$_instance;
        }
    
        /**
         * Redis数据库是否连接成功
         * @return bool|string
         */
        public static function connect()
        {
            // 如果连接资源不存在,则进行资源连接
            if (!self::$_connectSource)
            {
                //@return bool TRUE on success, FALSE on error.
                self::$_connectSource = self::getInstance()->connect(C('MASTER.HOST'), C('MASTER.PORT'), C('MASTER.TIMEOUT'));
                // 没有资源返回
                if (!self::$_connectSource)
                {
                    return 'Redis Server Connection Fail';
                }
            }
            return self::$_connectSource;
        }
    
        /**
         * 私有化克隆函数,防止类外克隆对象
         */
        private function __clone()
        {
            // TODO: Implement __clone() method.
        }
    
    
        /**
         * @return Redis
         * @static
         */
        public static function test()
        {
            return 'test';
        }
  • 相关阅读:
    CentOS 下搭建Jenkins
    SRVE0255E: 尚未定义要处理 ***的 Web 组/虚拟主机。
    WebSphere Application Server中manageprofiles的使用
    WAS 与IHS集成问题
    CentOS ln 链接
    VIM常见命令
    虚拟机VM下CentOS7部署WASND9+HTTP9
    CentOS7下安装GUI图形界面
    CentOS 系统时间与硬件时间
    hive 排序和聚集
  • 原文地址:https://www.cnblogs.com/tinywan/p/5903919.html
Copyright © 2011-2022 走看看