安装: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. 学习Uml开始

    Um的全称是 Unified Modeling Language, 统一建模语言,uml可以帮助我们做软件需求和软件设计的工作, 1.1UML的定义 UML是一种通用的可视化建模语言,是一种标准化的用 ...

  2. JavaWeb应用开发架构浅谈

    本文就我所经历和使用过的技术和框架, 讨论 Java / Javascript 技术组合构成的Web 应用架构. 一. 概述 Web 应用架构可以划分为两大子系统:前端子系统和后台子系统. 前端子系统 ...

  3. Process and Thread

    A process is a completely independent program that has its own address space, while a thread is a se ...

  4. EasyUI中在表单提交之前进行验证

    使用EasyUi我们可以在客户端表单提交之前进行验证,过程如下:只需在onSubmit的时候使用return  $("#form1").form('validate')方法即可,E ...

  5. Internship-ZOJ2532(网络流求割边)

    Internship Time Limit: 5 Seconds      Memory Limit: 32768 KB CIA headquarter collects data from acro ...

  6. LPCTSTR 用法

    L表示long指针       这是为了兼容Windows  3.1等16位操作系统遗留下来的,在win32中以及其他的32为操作系统中,  long指针和near指针及far修饰符都是为了兼容的作用 ...

  7. draggable datagrid columns

    $.extend($.fn.datagrid.methods,{ columnMoving: function(jq){ return jq.each(function(){ var target = ...

  8. 系统巡警 v1.2 系统行为分析神器

    系统巡警,是一款安全辅助软件,可以帮助系统维护人员或安全研究人员观察系统运行情况,包括进程启动与销毁记录.模块加载记录.线程启动与销毁记录.系统服务创建修改和删除记录.文件与文件夹的增加删除和修改记录 ...

  9. sd 卡驱动--基于高通平台

    点击打开链接 内容来自以下博客: http://blog.csdn.net/qianjin0703/article/details/5918041 Linux设备驱动子系统第二弹 - SD卡 (有介绍 ...

  10. 关于js执行顺序

    http://www.cnblogs.com/sanshi/archive/2011/02/28/1967367.html http://mtnt2008.iteye.com/blog/701981 ...