热搜:NVER node 开发 php

PHP RedisSingleton

2024-07-20 15:20:02
PHP RedisSingleton

<?php

/**
* Created by PhpStorm.
* User: xiongzai
* Date: 2016/5/17
* Time: 16:31
*/

namespace Think;

class RedisSingleton {

public $redis = null;

static protected $ins=null;

final protected function __construct(){
self::setRedis();
}

private function __clone(){}

static public function getInstance(){
if (self::$ins instanceof self) {
return self::$ins;
}
self::$ins=new self();
return self::$ins;
}


private function setRedis(){
try{
$redis = new \Redis();
$redis->connect( C('REDIS_HOST'), C('REDIS_PORT') );
$redis->auth( C('REDIS_AUTH') );
$this->redis = $redis;
unset($redis);
}catch(Exception $e){
echo $e->getMessage().'
';
}
}

}