Skip to content

Instantly share code, notes, and snippets.

@Tairy
Created January 8, 2018 07:54
Show Gist options
  • Save Tairy/0349dbfd4c4af01321c5700d00cf8b8b to your computer and use it in GitHub Desktop.
Save Tairy/0349dbfd4c4af01321c5700d00cf8b8b to your computer and use it in GitHub Desktop.
<?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