Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/guide-zh-CN/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Yii 2 Redis 缓存,会话和活动记录
===============================================

此扩展为 Yii2 framework 提供了 [redis](http://redis.io/) 键-值存储的支持。
包含了一个 `Cache` 和 `Session` 存储句柄并且实现了 `ActiveRecord` 模式,允许
你存储活动记录到 redis 中。


入门指南
---------------

* [安装](installation.md)

用法
-----

* [活动记录的使用](usage-ar.md)
* [直接使用命令](usage-commands.md)

附加主题
-----------------

* [缓存组件的使用](topics-cache.md)
* [会话组件的使用](topics-session.md)
42 changes: 42 additions & 0 deletions docs/guide-zh-CN/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
安装
============

## 需求

redis 2.6.12 版本是所有部件正常工作所必需的。

## 获取 Composer 安装包

安装此扩展的首选方式是通过 [composer](http://getcomposer.org/download/)。

可以运行

```
php composer.phar require --prefer-dist yiisoft/yii2-redis
```

或者添加

```json
"yiisoft/yii2-redis": "~2.0.0"
```

在 `composer.json` 文件中的必要部分。

## 配置应用程序

使用此扩展时,需要在你的应用程序配置中配置 Connection 类:

```php
return [
//....
'components' => [
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
]
];
```
37 changes: 37 additions & 0 deletions docs/guide-zh-CN/topics-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
缓存组件的使用
=========================

为了使用 `Cache` 组件,如 [安装](installation.md) 章节中所描述的,除了配置连接,
你也需要配置 `yii\redis\Cache` 中的 `cache` 组件:

```php
return [
//....
'components' => [
// ...
'cache' => [
'class' => 'yii\redis\Cache',
],
]
];
```

如果你只使用 redis 缓存(即,不使用它的活动记录或者会话),您还可以配置缓存组件内的
连接参数(在这种情况下,不需要配置连接应用程序的组件):

```php
return [
//....
'components' => [
// ...
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
],
]
];
```
37 changes: 37 additions & 0 deletions docs/guide-zh-CN/topics-session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
会话组件的使用
===========================

为了使用 `Session` 组件,如 [安装](installation.md) 章节中所描述的,除了配置连接,
你也需要配置 `yii\redis\Session` 中的 `session` 组件:

```php
return [
//....
'components' => [
// ...
'session' => [
'class' => 'yii\redis\Session',
],
]
];
```

如果你只使用 redis 会话(即,不使用它的活动记录或者缓存),您还可以配置会话组件内的
连接参数(在这种情况下,不需要配置连接应用程序的组件):

```php
return [
//....
'components' => [
// ...
'session' => [
'class' => 'yii\redis\Session',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
],
]
];
```
64 changes: 64 additions & 0 deletions docs/guide-zh-CN/usage-ar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
活动记录的使用
======================

对于如何使用 yii 的活动记录一般信息请参阅 [指南](https://github.com/yiisoft/yii2/blob/master/docs/guide/active-record.md)。

定义一个 redis 活动记录类,你的记录类需要继承自 [[yii\redis\ActiveRecord]] 并且
至少实现 `attributes()` 方法来定义记录的属性。
一个没有指定默认值,默认为 `id` 的主键可以通过 [[yii\redis\ActiveRecord::primaryKey()]] 定义。
主键是属性中必要的一部分,所以请确保你有一个 `id` 属性定义的,
如果你没有指定自己的主键。

以下是一个 `Customer` 的实例模型:

```php
class Customer extends \yii\redis\ActiveRecord
{
/**
* @return array 此记录的属性列表
*/
public function attributes()
{
return ['id', 'name', 'address', 'registration_date'];
}

/**
* @return ActiveQuery 定义一个关联到 Order 的记录(可以在其它数据库中,例如 elasticsearch 或者 sql)
*/
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}

/**
* 定义一个修改 `$query` 的范围返回有效(status = 1)的客户。
*/
public static function active($query)
{
$query->andWhere(['status' => 1]);
}
}
```

redis 活动记录的一般用法和数据库活动记录非常相似,正如
[指南](https://github.com/yiisoft/yii2/blob/master/docs/guide/active-record.md) 中所描述的。
它支持相同的界面和功能,除了以下限制:

- redis 不支持 SQL 查询的 API 仅限于以下方法:
`where()`,`limit()`,`offset()`,`orderBy()` 和 `indexBy()`。
(orderBy() 尚未实现:[#1305](https://github.com/yiisoft/yii2/issues/1305))
- `via`-关系不能通过在 redis 中没有的表定义。你只能通过其他记录来定义关系。

另外,也可以定义从 redis 的活动记录关系到正常的活动记录类,反之亦然。

使用实例:

```php
$customer = new Customer();
$customer->attributes = ['name' => 'test'];
$customer->save();
echo $customer->id; // 如果没有明确设置 id 会自动递增

$customer = Customer::find()->where(['name' => 'test'])->one(); // 通过 query 查找
$customer = Customer::find()->active()->all(); // 通过 query 查找全部(使用 `active` 范围)
```
23 changes: 23 additions & 0 deletions docs/guide-zh-CN/usage-commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
直接使用命令行
=======================

Redis 有很多可以直接从连接中使用的有用的命令。在配置应用程序后,
如 [安装](installation.md) 所示,连接可以像下面这样获取:

```php
$redis = Yii::$app->redis;
```

完成之后可以执行如下命令。最通用的方法是使用 `executeCommand` 方法:

```php
$result = $redis->executeCommand('hmset', ['test_collection', 'key1', 'val1', 'key2', 'val2']);
```

每个命令都有相应的快捷方式支持,所以可以像下面这样代替以上的命令:

```php
$result = $redis->hmset(['test_collection', 'key1', 'val1', 'key2', 'val2']);
```

可用命令列表和他们的参数可参阅 [http://redis.io/commands](http://redis.io/commands)。