redis 类
来源:互联网 发布:财务管理数据库设计 编辑:程序博客网 时间:2024/06/08 10:33
公司需要刚封装的redis类,简单,好用
<?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...
0 0
- redis 类
- redis 类
- Redis工具类获取Redis
- 【Redis】Java之Redis工具类
- redis类的实现
- PHP Redis类操作
- Redis通用类
- PHP Redis类操作
- PHP Redis类操作
- PHP Redis类
- PHP Redis类操作
- php+redis缓存类
- redis 工具类
- redis通用工具类
- PHP Redis类操作
- redis 连接池类
- 简单封装Redis类
- php redis操作类
- angularjs中provider、factory和service三者的写法与区别
- Understanding Weak References
- Android ANR 分析解决方法
- pku2253 记录从初始到终点最小的路径(某一部分),迪杰斯特拉求最短路
- 通过java.net.URLConnection发送HTTP请求的方法
- redis 类
- Tomcat文件上传路径问题
- 如果截代码分享,请截出可以调试的整个部分!
- 主控-从属模式
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- selector和shape静态资源的使用
- STL系列十一 随机三趣题——随机重排,文件中随机取一行,生成N个随机数
- HDU 2232 机器人的舞蹈 矩阵乘法
- 讲义6:简单查询