Yii2-redis
安装:composer require --prefer-dist yiisoft/yii2-redis
redis 版本 >= 2.6.12
添加配置:
'components' => [
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
]
# ActiveRecord
继承自 \yii\redis\ActiveRecord 且必须实现 attributes() 方法
static active 方法 用来指定 查询条件
用法与自带的 ActiveRecord 相似 有以下不同
1、不支持SQL查询API 仅支持 where() limit() offset() orderBy() indexBy() (orderBy()暂未实现)
2、不能通过表来定义关系 redis中没有表
使用示例
class Customer extends \yii\redis\ActiveRecord
{
/**
* @return array the list of attributes for this record
*/
public function attributes()
{
return ['id', 'name', 'address', 'registration_date'];
} /**
* @return ActiveQuery defines a relation to the Order record (can be in other database, e.g. elasticsearch or sql)
*/
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
} /**
* Defines a scope that modifies the `$query` to return only active(status = 1) customers
*/
public static function active($query)
{
$query->andWhere(['status' => 1]);
}
} $customer = new Customer();
$customer->attributes = ['name' => 'test'];
$customer->save();
echo $customer->id; // id will automatically be incremented if not set explicitly $customer = Customer::find()->where(['name' => 'test'])->one(); // find by query
$customer = Customer::find()->active()->all(); // find all by query (using the `active` scope)
# 使用命令
$redis = Yii::$app->redis;
$result = $redis->executeCommand('hmset', ['test_collection', 'key1', 'val1', 'key2', 'val2']); //通用用法
$result = $redis->hmset('test_collection', 'key1', 'val1', 'key2', 'val2');//方法用法
可用命令及参数 参考 http://redis.io/commands.
#缓存组件
配置
'components' => [
// ...
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [ // 如果只用作缓存 需要配置此项
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
],
]
# SESSION
配置
'components' => [
// ...
'session' => [
'class' => 'yii\redis\Session',
'redis' => [ //如果只用作 session 需要配置
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
],
]
Yii2-redis的更多相关文章
- Yii2 Redis的使用
Yii2 redis扩展 下载 将下载的解压,改名为redis,放在vendor\yiisoft\yii2\目录下 包含Connection.php,ActiveRecord.php等文件 修改 ...
- Yii2 redis 使用方法
/** * 基于 yii2.0 redis使用方法 *///项目根目录命令行执行 composer require --prefer-dist yiisoft/yii2-redis; //在配置文件中 ...
- Yii2 redis与cache
原文地址:http://www.myexception.cn/php/1974979.html composer require yiisoft/yii2-redis 安装后使用超简单,打开 comm ...
- Yii2 redis 使用
首先要安装一下redis的扩展 composer require yiisoft/yii2-redis 在配置文件中添加redis配置 'components' => [ .... 'redis ...
- yii2redis安装
yii2 – redis 配置 转自:http://www.fancyecommerce.com/2016/05/03/yii2-redis-%E9%85%8D%E7%BD%AE/ 安装redis w ...
- Yii2框架与MongoDB拓展、Redis拓展的安装流程
@author 周煦辰 2016-03-21 这段时间新上了一个项目,使用的是Yii2框架.这里记录一下Yii2框架.Yii2-Mongo拓展.Yii2-Redis拓展等的安装流程.因为使用的系统是W ...
- yii2的redis扩展使用
yii2支持了redis扩展,不需要在本地下载php的扩展库就可以很好的使用 1.下载windows的redis安装包打开cmd,进入安装包目录,使用redis-server.exe redis.co ...
- Yii2.0源码阅读-PHP如何与redis通信?
PHP与Redis可以通过socket进行通信,前提是PHP需要实现Redis的协议 RESP协议描述: 字符串 \r\n : 表示一个正确的状态信息,具体信息是'+'后面的字符(Simple Str ...
- yii2.0 手动配置redis
手动安装yii2.0-redis扩展 1.点击下载:yii2.0-redis扩展 2.把下载的扩展文件放到vendor/yiisoft/下,命名为yii2-redis 3.修改vender/yiiso ...
- YII2 搭建redis拓展(教程)
安装redis扩展: 1.通过composer进行安装,到项目根目录cmd运行(推荐) php composer.phar require --prefer-dist yiisoft/yii2-red ...
随机推荐
- SQL语句中:UNION与UNION ALL的区别
有些人看到题目,瞬间觉得楼主也太弱了吧,这种问题也要拿出来写,这种问题 随便会点sql 的人基本都会 Union 是会删除冗余数据 Union ALL 不会删除冗余数据 将所有的结果都展现给用户 ...
- jqgrid定义多选操作
先定义: var SelectRowIndx; 然后加入以下属性: multiselect: true, onSelectRow: function () { SelectRowIndx = G ...
- C语言回顾-运算符和循环
1.运算符 连接操作数,构成表达式 按功能划分: 1)算术运算符 + - * / % 2)关系运算符 3)逻辑运算符 4)按位运算符 按操作数划分: 1)单目运算符 2)双目运算符 3)三目运算符 ...
- stack overflow--技术问答网站
转自:http://baike.baidu.com/link?url=eMR6Pwdk9IkauI5B3nZb2Yo3VUAcK6vQfrMpcSMPWqgH0ngqFkup3Gdr3t_s_yZe_ ...
- WIFI驱动的移植 realtek 8188
一般我们拿到的android源代码中wifi应用层部分是好的, 主要是wifi芯片的驱动要移植并添加进去. wifi驱动的移植, 以realtek的8188etv为例到官网下载相应的驱动, 解压后可以 ...
- [课程设计]Scrum 2.3 多鱼点餐系统开发进度 (订单一览设计)
Scrum 2.3 多鱼点餐系统开发进度 (订单一览设计) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系统 ...
- js与jquery的用法
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />后面加上 ...
- [问题2014S15] 复旦高等代数II(13级)每周一题(第十五教学周)
[问题2014S15] 设 \(O\) 为 \(n\) 阶正交阵,\(A=\mathrm{diag}\{a_1,a_2,\cdots,a_n\}\) 为实对角阵, 证明: 方阵 \(OA\) 的特征 ...
- 教你解决Sublime Text中文乱码问题
教你解决Sublime Text中文乱码问题[转载自成长的企鹅] Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦. ...
- python: 模块发布
一.准备发布 1.为模块文件创建一个文件夹,并将模块文件复制到这个文件中(一般,文件夹的名字和模块的名字一样) 2.在文件夹中创建一个名为『setup.py』的文件,内容如下: #encoding:u ...