安装: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的更多相关文章

  1. Yii2 Redis的使用

    Yii2 redis扩展    下载 将下载的解压,改名为redis,放在vendor\yiisoft\yii2\目录下 包含Connection.php,ActiveRecord.php等文件 修改 ...

  2. Yii2 redis 使用方法

    /** * 基于 yii2.0 redis使用方法 *///项目根目录命令行执行 composer require --prefer-dist yiisoft/yii2-redis; //在配置文件中 ...

  3. Yii2 redis与cache

    原文地址:http://www.myexception.cn/php/1974979.html composer require yiisoft/yii2-redis 安装后使用超简单,打开 comm ...

  4. Yii2 redis 使用

    首先要安装一下redis的扩展 composer require yiisoft/yii2-redis 在配置文件中添加redis配置 'components' => [ .... 'redis ...

  5. yii2redis安装

    yii2 – redis 配置 转自:http://www.fancyecommerce.com/2016/05/03/yii2-redis-%E9%85%8D%E7%BD%AE/ 安装redis w ...

  6. Yii2框架与MongoDB拓展、Redis拓展的安装流程

    @author 周煦辰 2016-03-21 这段时间新上了一个项目,使用的是Yii2框架.这里记录一下Yii2框架.Yii2-Mongo拓展.Yii2-Redis拓展等的安装流程.因为使用的系统是W ...

  7. yii2的redis扩展使用

    yii2支持了redis扩展,不需要在本地下载php的扩展库就可以很好的使用 1.下载windows的redis安装包打开cmd,进入安装包目录,使用redis-server.exe redis.co ...

  8. Yii2.0源码阅读-PHP如何与redis通信?

    PHP与Redis可以通过socket进行通信,前提是PHP需要实现Redis的协议 RESP协议描述: 字符串 \r\n : 表示一个正确的状态信息,具体信息是'+'后面的字符(Simple Str ...

  9. yii2.0 手动配置redis

    手动安装yii2.0-redis扩展 1.点击下载:yii2.0-redis扩展 2.把下载的扩展文件放到vendor/yiisoft/下,命名为yii2-redis 3.修改vender/yiiso ...

  10. YII2 搭建redis拓展(教程)

    安装redis扩展: 1.通过composer进行安装,到项目根目录cmd运行(推荐) php composer.phar require --prefer-dist yiisoft/yii2-red ...

随机推荐

  1. Web应用程序项目以配置使用IIS。未找到Web服务器

    解决办法 右键编辑该Web项目的csproj文件 把UserIIS改为False.或者在IIS服务器里面配置一个IISUrl里面的地址 如图

  2. ajax+jquery+ashx如何实现上传文件

    第一:建立Default.aspx页面 <html> <head runat="server"> <title>ajax图片上传</tit ...

  3. js子窗体、父窗体方法互调

    var childWindow = $("#editFrame")[0].contentWindow;//获取子窗体的window对象. childWindow.subForm() ...

  4. 自定义view

    这两篇文章不可错过,是最靠谱的基础文献.总的来说,如果想完全定制,就继承与于View类:如果只是在原有控件基础上拓展,那就继承TextView.Button或者LinearLayout等.接下来,就以 ...

  5. "未能找到类型或命名空间名称",引用dll的时候出错

    当前项目是.net2.0框架,引用的dll是 .net 4.5框架,引用后编译时报错“未能找到类型或命名空间名称”. 当前项目 右键-->应用程序-->目标框架 改为 .net frame ...

  6. Ugly Number II

    注意负数,所以要使用long,而不能用int 详细解释 请参见http://www.cnblogs.com/julie-yang/p/5147460.html #include<vector&g ...

  7. [转]Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序

    转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/13/251101.html 本系列教程将详细介绍Struts 1.x的基本原理和使用方法, ...

  8. Linux常用命令(三)

    1.top 说明:即时显示 process 的动态 语法格式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b]基本参数:d : 改变显示的更新速度,或是在交谈 ...

  9. Redundant Paths-POJ3177(并查集+双连通分量)

    Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...

  10. 浙江理工2015.12校赛-F Landlocked

    Landlocked Time Limit: 5 Sec Memory Limit: 128 MB Submit: 288 Solved: 39 Description Canada is not a ...