Home >Backend Development >PHP Tutorial >PHP RedisSingleton

PHP RedisSingleton

WBOY
WBOYOriginal
2016-06-23 13:06:041024browse

<?php<br /><br />/**<br /> * Created by PhpStorm.<br /> * User: xiongzai<br /> * Date: 2016/5/17<br /> * Time: 16:31<br /> */<br /><br />namespace Think;<br /><br />class RedisSingleton {<br /><br />    public $redis = null;<br /><br />    static protected $ins=null;<br /><br />    final protected function __construct(){<br />        self::setRedis();<br />    }<br /><br />    private function __clone(){}<br /><br />    static public function getInstance(){<br />        if (self::$ins instanceof self) {<br />            return self::$ins;<br />        }<br />        self::$ins=new self();<br />        return self::$ins;<br />    }<br /><br /><br />     private function setRedis(){<br />        try{<br />            $redis = new \Redis();<br />            $redis->connect( C('REDIS_HOST'), C('REDIS_PORT') );<br />            $redis->auth( C('REDIS_AUTH') );<br />            $this->redis = $redis;<br />            unset($redis);<br />        }catch(Exception $e){<br />            echo $e->getMessage().'<br/>';<br />        }<br />    }<br /><br />}<br /><br />

Statement:
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
Previous article:php 数组处理Next article:mysql数据库编码问题