Skip to content

Commit d4302ec

Browse files
committed
Merge pull request #34 from yiichina/master
Add zh-CN translation
2 parents 234d6fd + 167bec3 commit d4302ec

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

docs/guide-zh-CN/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Yii 2 Redis 缓存,会话和活动记录
2+
===============================================
3+
4+
此扩展为 Yii2 framework 提供了 [redis](http://redis.io/) 键-值存储的支持。
5+
包含了一个 `Cache``Session` 存储句柄并且实现了 `ActiveRecord` 模式,允许
6+
你存储活动记录到 redis 中。
7+
8+
9+
入门指南
10+
---------------
11+
12+
* [安装](installation.md)
13+
14+
用法
15+
-----
16+
17+
* [活动记录的使用](usage-ar.md)
18+
* [直接使用命令](usage-commands.md)
19+
20+
附加主题
21+
-----------------
22+
23+
* [缓存组件的使用](topics-cache.md)
24+
* [会话组件的使用](topics-session.md)

docs/guide-zh-CN/installation.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
安装
2+
============
3+
4+
## 需求
5+
6+
redis 2.6.12 版本是所有部件正常工作所必需的。
7+
8+
## 获取 Composer 安装包
9+
10+
安装此扩展的首选方式是通过 [composer](http://getcomposer.org/download/)
11+
12+
可以运行
13+
14+
```
15+
php composer.phar require --prefer-dist yiisoft/yii2-redis
16+
```
17+
18+
或者添加
19+
20+
```json
21+
"yiisoft/yii2-redis": "~2.0.0"
22+
```
23+
24+
`composer.json` 文件中的必要部分。
25+
26+
## 配置应用程序
27+
28+
使用此扩展时,需要在你的应用程序配置中配置 Connection 类:
29+
30+
```php
31+
return [
32+
//....
33+
'components' => [
34+
'redis' => [
35+
'class' => 'yii\redis\Connection',
36+
'hostname' => 'localhost',
37+
'port' => 6379,
38+
'database' => 0,
39+
],
40+
]
41+
];
42+
```

docs/guide-zh-CN/topics-cache.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
缓存组件的使用
2+
=========================
3+
4+
为了使用 `Cache` 组件,如 [安装](installation.md) 章节中所描述的,除了配置连接,
5+
你也需要配置 `yii\redis\Cache` 中的 `cache` 组件:
6+
7+
```php
8+
return [
9+
//....
10+
'components' => [
11+
// ...
12+
'cache' => [
13+
'class' => 'yii\redis\Cache',
14+
],
15+
]
16+
];
17+
```
18+
19+
如果你只使用 redis 缓存(即,不使用它的活动记录或者会话),您还可以配置缓存组件内的
20+
连接参数(在这种情况下,不需要配置连接应用程序的组件):
21+
22+
```php
23+
return [
24+
//....
25+
'components' => [
26+
// ...
27+
'cache' => [
28+
'class' => 'yii\redis\Cache',
29+
'redis' => [
30+
'hostname' => 'localhost',
31+
'port' => 6379,
32+
'database' => 0,
33+
],
34+
],
35+
]
36+
];
37+
```

docs/guide-zh-CN/topics-session.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
会话组件的使用
2+
===========================
3+
4+
为了使用 `Session` 组件,如 [安装](installation.md) 章节中所描述的,除了配置连接,
5+
你也需要配置 `yii\redis\Session` 中的 `session` 组件:
6+
7+
```php
8+
return [
9+
//....
10+
'components' => [
11+
// ...
12+
'session' => [
13+
'class' => 'yii\redis\Session',
14+
],
15+
]
16+
];
17+
```
18+
19+
如果你只使用 redis 会话(即,不使用它的活动记录或者缓存),您还可以配置会话组件内的
20+
连接参数(在这种情况下,不需要配置连接应用程序的组件):
21+
22+
```php
23+
return [
24+
//....
25+
'components' => [
26+
// ...
27+
'session' => [
28+
'class' => 'yii\redis\Session',
29+
'redis' => [
30+
'hostname' => 'localhost',
31+
'port' => 6379,
32+
'database' => 0,
33+
],
34+
],
35+
]
36+
];
37+
```

docs/guide-zh-CN/usage-ar.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
活动记录的使用
2+
======================
3+
4+
对于如何使用 yii 的活动记录一般信息请参阅 [指南](https://github.com/yiisoft/yii2/blob/master/docs/guide/active-record.md)
5+
6+
定义一个 redis 活动记录类,你的记录类需要继承自 [[yii\redis\ActiveRecord]] 并且
7+
至少实现 `attributes()` 方法来定义记录的属性。
8+
一个没有指定默认值,默认为 `id` 的主键可以通过 [[yii\redis\ActiveRecord::primaryKey()]] 定义。
9+
主键是属性中必要的一部分,所以请确保你有一个 `id` 属性定义的,
10+
如果你没有指定自己的主键。
11+
12+
以下是一个 `Customer` 的实例模型:
13+
14+
```php
15+
class Customer extends \yii\redis\ActiveRecord
16+
{
17+
/**
18+
* @return array 此记录的属性列表
19+
*/
20+
public function attributes()
21+
{
22+
return ['id', 'name', 'address', 'registration_date'];
23+
}
24+
25+
/**
26+
* @return ActiveQuery 定义一个关联到 Order 的记录(可以在其它数据库中,例如 elasticsearch 或者 sql)
27+
*/
28+
public function getOrders()
29+
{
30+
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
31+
}
32+
33+
/**
34+
* 定义一个修改 `$query` 的范围返回有效(status = 1)的客户。
35+
*/
36+
public static function active($query)
37+
{
38+
$query->andWhere(['status' => 1]);
39+
}
40+
}
41+
```
42+
43+
redis 活动记录的一般用法和数据库活动记录非常相似,正如
44+
[指南](https://github.com/yiisoft/yii2/blob/master/docs/guide/active-record.md) 中所描述的。
45+
它支持相同的界面和功能,除了以下限制:
46+
47+
- redis 不支持 SQL 查询的 API 仅限于以下方法:
48+
`where()``limit()``offset()``orderBy()``indexBy()`
49+
(orderBy() 尚未实现:[#1305](https://github.com/yiisoft/yii2/issues/1305))
50+
- `via`-关系不能通过在 redis 中没有的表定义。你只能通过其他记录来定义关系。
51+
52+
另外,也可以定义从 redis 的活动记录关系到正常的活动记录类,反之亦然。
53+
54+
使用实例:
55+
56+
```php
57+
$customer = new Customer();
58+
$customer->attributes = ['name' => 'test'];
59+
$customer->save();
60+
echo $customer->id; // 如果没有明确设置 id 会自动递增
61+
62+
$customer = Customer::find()->where(['name' => 'test'])->one(); // 通过 query 查找
63+
$customer = Customer::find()->active()->all(); // 通过 query 查找全部(使用 `active` 范围)
64+
```

docs/guide-zh-CN/usage-commands.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
直接使用命令行
2+
=======================
3+
4+
Redis 有很多可以直接从连接中使用的有用的命令。在配置应用程序后,
5+
[安装](installation.md) 所示,连接可以像下面这样获取:
6+
7+
```php
8+
$redis = Yii::$app->redis;
9+
```
10+
11+
完成之后可以执行如下命令。最通用的方法是使用 `executeCommand` 方法:
12+
13+
```php
14+
$result = $redis->executeCommand('hmset', ['test_collection', 'key1', 'val1', 'key2', 'val2']);
15+
```
16+
17+
每个命令都有相应的快捷方式支持,所以可以像下面这样代替以上的命令:
18+
19+
```php
20+
$result = $redis->hmset(['test_collection', 'key1', 'val1', 'key2', 'val2']);
21+
```
22+
23+
可用命令列表和他们的参数可参阅 [http://redis.io/commands](http://redis.io/commands)

0 commit comments

Comments
 (0)