redis 类
2015-12-03 12:10
288 查看
公司需要刚封装的redis类,简单,好用
使用方法
配置文件使用的是yaml
<?php /** * redis 封装类 * User: gaojisheng * Date: 15/12/3 * Time: 上午9:28 */ class My_Redis { //redis内部对象 protected $_instance; /** * @param string|null $name 服务器名 */ public function __construct(string $name = null) { if($name == null) { $config = $this->_getConfig($name); } $this->_connect($config); } public function __call($func, array $params) { return call_user_func_array(array($this->_instance, $func), $params); } /** * 连接redis * @param array $config 配置 */ protected function _connect(array $config) { $this->_instance = new Redis(); $this->_instance->connect($config['host'], $config['port']); if (isset($config['password']) && $config['password'] != null) { $this->_instance->auth($config['password']); } $this->select($config['db']); } /** * 选择库 * @param $db 库 */ public function select($db) { $this->_instance->select($db); } /** * 获取配置文件 * @param string|null $name 服务器名 * @return array 配置信息 */ protected function _getConfig(string $name = null) { $configs = yaml_parse_file(__DIR__.'/redis.yaml'); if($name == null) { foreach($configs as $key => $item) { $name = $key; } } if(isset($configs[$name])) { return array_merge($configs[$name], array('title' => $name)); } return array(); } } /** * 对外提供的方法 * @param string|null $name 服务器名 * @return My_Redis redis对象 */ function create(string $name = null) { return new My_Redis(); } /** * 单例对外提供的方法 * @param string|null $name 服务器名 * @return My_Redis redis对象 */ function instance(string $name = null) { static $instances = array(); if(empty($instances[$name])) { return $instances[$name] = create($name); } return $instances[$name]; }
使用方法
print_r(create()->get("test")); print_r(instance()->get("test"));
配置文件使用的是yaml
--- rite: host: 127.0.0.1 port: 6379 db: 1 password: "" rite2: host: 127.0.0.1 port: 6379 db: 1 password: gaojstest ...
相关文章推荐
- Python动态类型的学习---引用的理解
- redis安装问题小结
- Redis偶发连接失败案例实战记录
- 土人系列AS入门教程 -- 对象篇
- C#托管堆对象实例包含内容分析
- Redis中实现查找某个值的范围
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- C#实现获取不同对象中名称相同属性的方法
- redis中修改配置文件中的端口号 密码方法
- 在Ruby on Rails上使用Redis Store的方法
- javascript asp教程第十一课--Application 对象
- Redis和Memcache的区别总结
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- VBS教程:对象-正则表达式(RegExp)对象
- C#检查指定对象是否存在于ArrayList集合中的方法
- 在Node.js应用中使用Redis的方法简介