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 ...
随机推荐
- 获取访问者ip的方法
package com.mi.util; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.S ...
- SQL2005中的事务与锁定(五)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone - ...
- xib中的view对iPhone和iPad自适应
1 This worked for me: Make a copy of the .xib in the Finder. Open the copied file in a text edito ...
- 使用Netty收发二进制报文问题记
1.java二进制编解码 byteBuffer.flip() byteBuffer.getInt() 与 byteBuf.getInt(11) 2.粘包拆包问题 LengthFieldBasedFra ...
- React,js实现分页的案列
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- elasticsearch之python备份
一:elasticsearch原理 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功 ...
- winform基础,主要控件简单介绍,以及小练习
WinForm - C/S B/S 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执 ...
- 查看SQL SERVER数据库运行参数和连接数
---查看当前数据库系统所有请求情况.我只列出了我认为比较重要有助于我解决问题的字段. SELECT ds.session_id, ds.status, Db_name(dr.database_id) ...
- Beaglebone Black–I2C 接 BMP280 获取当前温度
我有两个含温度传感的模块,一个是AOSONG 奥松电子的 AM2320 温度湿度,另一个是九轴里面的 Bosch BMP280.由于 AM2320 用 I2C MODBUS,直接用 I2C Tools ...
- C语言运算符和优先级
关于C语言运算符和优先级,经整理众多博客资料汇入自己的实战,如下: a.算术运算 C语言一共有34种运算符,包括常见的加减乘除运算. 1) 加法:+ 还可以表 ...