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';
        }
  • 相关阅读:
    uva 10369 Arctic Network
    uvalive 5834 Genghis Khan The Conqueror
    uvalive 4848 Tour Belt
    uvalive 4960 Sensor Network
    codeforces 798c Mike And Gcd Problem
    codeforces 796c Bank Hacking
    codeforces 768c Jon Snow And His Favourite Number
    hdu 1114 Piggy-Bank
    poj 1276 Cash Machine
    bzoj 2423 最长公共子序列
  • 原文地址:https://www.cnblogs.com/tinywan/p/5903919.html
Copyright © 2011-2022 走看看