<?php
* redis初始化类库
* @author Alex <cyy0523xc@gmail.com>
* @copyright IBBD.net
* @see
* @todo
* @version 20141104
*/
namespace Common\Model;
class RedisModel
{
private $handler = null;
* 获取Redis缓存对象
* @param array $options redis连接所需要的参数
* @return Redis
*/
public static function getInstance(array $options = null)
{
static $instance = array();
if(empty($options)) {
$options = C('CACHE.REDIS_CONFIG');
}
$guid = to_guid_string($options);
if (!isset($instance[$guid])) {
$obj = new self($options);
$instance[$guid] = $obj->getRedis();
}
return $instance[$guid];
}
* 构造函数
* @param array $options redis需要的参数
*/
protected function __construct(array $options)
{
if ( !extension_loaded('redis') ) {
error_log('NOT_SUPPORT:redis');
}
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->handler = new \Redis;
$options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
isset($options['db']) && $this->select($options['db']);
}
* 获取redis处理对象
* @return Redis
*/
protected function getRedis()
{
return $this->handler;
}
}