Created
January 8, 2018 07:54
-
-
Save Tairy/0349dbfd4c4af01321c5700d00cf8b8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: tairy | |
* Date: 05/01/2018 | |
* Time: 20:23 | |
* | |
* Redis 单例操作 | |
*/ | |
class RedisManager | |
{ | |
private static $redisInstance; | |
/** | |
* 私有化构造函数 | |
* 原因:防止外界调用构造新的对象 | |
*/ | |
private function __construct() | |
{ | |
} | |
/** | |
* 获取redis连接的唯一出口 | |
*/ | |
static public function getRedisConn() | |
{ | |
if (!self::$redisInstance instanceof self) { | |
self::$redisInstance = new self; | |
} | |
// 获取当前单例 | |
$temp = self::$redisInstance; | |
// 调用私有化方法 | |
return $temp->connRedis(); | |
} | |
/** | |
* 连接ocean 上的redis的私有化方法 | |
* @return Redis | |
*/ | |
static private function connRedis() | |
{ | |
$redisOcean = NULL; | |
try { | |
$config = Yaf_Registry::get('config')->redis->toArray(); | |
$redisOcean = new Redis(); | |
$redisOcean->connect($config['host'], $config['port']); | |
$redisOcean->auth($config['password']); | |
$redisOcean->select(0); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
return $redisOcean; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment