Skip to content

Commit 9f7e5c5

Browse files
committed
Merge branch 'master' of https://github.com/golaravel/laravel4.2docs into upstream
2 parents 2c9e9af + fefe7db commit 9f7e5c5

File tree

6 files changed

+692
-0
lines changed

6 files changed

+692
-0
lines changed

cn/cache.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# 缓存
2+
3+
- [配置](#configuration)
4+
- [缓存用法](#cache-usage)
5+
- [递增 & 递减](#increments-and-decrements)
6+
- [缓存标签](#cache-tags)
7+
- [数据库缓存](#database-cache)
8+
9+
<a name="configuration"></a>
10+
## 配置
11+
12+
Laravel对不同的缓存系统提供了统一的API. 请在 `app/config/cache.php` 文件中配置缓存信息. 你可以在此文件中指定整个应用程序默认使用哪种缓存驱动. Laravel支持系统以外的流行后端缓存如 [Memcached](http://memcached.org)[Redis](http://redis.io).
13+
14+
缓存配置文件还包含了其他配置项, 都已记录在文件中, 请详细阅读. 默认情况下, Laravel被配置为使用 'file' 缓存驱动, 它将缓存数据序列化的存储在文件系统中, 对于大型的应用, 我们建议你使用内存缓存, 如 Memcached 或 APC.
15+
16+
<a name="cache-usage"></a>
17+
## 缓存用法
18+
19+
#### 将某一条数据存入缓存
20+
21+
Cache::put('key', 'value', $minutes);
22+
23+
#### 使用Carbon对象并设置过期时间
24+
25+
$expiresAt = Carbon::now()->addMinutes(10);
26+
27+
Cache::put('key', 'value', $expiresAt);
28+
29+
#### 当一条数据不在缓存中才存储
30+
31+
Cache::add('key', 'value', $minutes);
32+
33+
如果该数据实际上 **已经添加** 到缓存中, 那么 `add` 方法将返回 `true` . 否则, 此方法将返回 `false`.
34+
35+
#### 检查缓存中是否存在某个key对应的数据
36+
37+
if (Cache::has('key'))
38+
{
39+
//
40+
}
41+
42+
#### 从缓存中取得一条key对应的数据
43+
44+
$value = Cache::get('key');
45+
46+
#### 从缓存中取得数据, 如果不存在, 则返回指定的默认值
47+
48+
$value = Cache::get('key', 'default');
49+
50+
$value = Cache::get('key', function() { return 'default'; });
51+
52+
#### 在缓存中永久存储数据
53+
54+
Cache::forever('key', 'value');
55+
56+
有时候你可能希望从缓存中取得数据, 并且数据不存在时还可以存储一个默认值, 这时可以使用 `Cache::remember` 方法:
57+
58+
$value = Cache::remember('users', $minutes, function()
59+
{
60+
return DB::table('users')->get();
61+
});
62+
63+
你还可以结合使用 `remember``forever` 方法:
64+
65+
$value = Cache::rememberForever('users', function()
66+
{
67+
return DB::table('users')->get();
68+
});
69+
70+
注意: 所有项目都是序列化的存储在缓存中, 所以你可以自由存储任何类型的数据.
71+
72+
#### 在缓存中拉出一条数据
73+
74+
如果你需要从缓存中先取出一条数据, 然后删除它, 你可以使用 `pull` 方法:
75+
76+
$value = Cache::pull('key');
77+
78+
#### 从缓存中删除某条数据
79+
80+
Cache::forget('key');
81+
82+
<a name="increments-and-decrements"></a>
83+
## 递增 & 递减
84+
85+
除了 `file``database` , 其他驱动都支持 `increment` and `decrement` 操作:
86+
87+
#### 递增某一个值
88+
89+
Cache::increment('key');
90+
91+
Cache::increment('key', $amount);
92+
93+
#### 递减某一个值
94+
95+
Cache::decrement('key');
96+
97+
Cache::decrement('key', $amount);
98+
99+
<a name="cache-tags"></a>
100+
## 缓存标签
101+
102+
> **注意:** 使用 `file``database` 缓存驱动时不支持缓存标签. 此外, 在使用多个缓存标签时它们将存储为 "forever", 使用一个如 `memcached` 的驱动性能将会是最好, 它会自动清除过时的记录.
103+
104+
#### 访问一个标记的缓存
105+
106+
缓存标签允许你在缓存中标记相关的项目, 刷新指定名称标记的所有缓存. 要访问一个标记的缓存, 可以使用 `tags` 方法.
107+
108+
你可以通过传递一个标记名称的有序列表作为参数, 或是一个标记名称的有序数数组, 来存储一个标记的缓存:
109+
110+
Cache::tags('people', 'authors')->put('John', $john, $minutes);
111+
112+
Cache::tags(array('people', 'artists'))->put('Anne', $anne, $minutes);
113+
114+
你可以在任何缓存存储方法中组合使用标签, 包括`remember`, `forever`, 和 `rememberForever`. 你也可以从标记的缓存中访问已缓存的项目, 以及使用其它的缓存方法, 如 `increment``decrement`.
115+
116+
#### 从标记的缓存中访问项目
117+
118+
通过与保存时所用相同的标签, 作为参数列表来访问一个标记的缓存.
119+
120+
$anne = Cache::tags('people', 'artists')->get('Anne');
121+
122+
$john = Cache::tags(array('people', 'authors'))->get('John');
123+
124+
你可以通过一个名称或名称的列表来刷新所有的项目. 例如, 下面的语句将移除标记有任何'people', 'authors', 或两者都有的所有缓存. 所以, 无论是“Anne”和“John”将从缓存中被移除:
125+
126+
Cache::tags('people', 'authors')->flush();
127+
128+
相比之下, 下面的语句将移除标记中只包含 'authors' 的缓存, 因此 "John" 将被移除, 但不影响 "Anne".
129+
130+
Cache::tags('authors')->flush();
131+
132+
<a name="database-cache"></a>
133+
## 数据库缓存
134+
135+
当使用'数据库'缓存驱动时, 你将需要设置一个表来存储缓存数据. 以下是一个使用 `Schema` 声明的例子:
136+
137+
Schema::create('cache', function($table)
138+
{
139+
$table->string('key')->unique();
140+
$table->text('value');
141+
$table->integer('expiration');
142+
});

cn/facades.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Facades
2+
3+
- [介绍](#introduction)
4+
- [说明](#explanation)
5+
- [实际用例](#practical-usage)
6+
- [创建 Facades](#creating-facades)
7+
- [模拟 Facades](#mocking-facades)
8+
- [Facade 类参考](#facade-class-reference)
9+
10+
<a name="introduction"></a>
11+
## 介绍
12+
13+
Facades(一种设计模式,通常翻译为外观模式)提供了一个"static"(静态)接口去访问注册到[IoC 容器](/docs/4.2/ioc)中的类 。Laravel 含有很多 facades,你可能不知道其实你在某些地方已经使用过它们了。Laravel的"facades"就像"静态代理方法",为IoC容器中的类服务,相比传统的静态方法,在保持了更强的可测试性和灵活性的同时,它提供更简洁,更富有表现力的语法。
14+
15+
有时候, 你可能想为你的应用程序或包创建自己的 facades, 所以,让我们来讨论一下如何开发和使用这些类。
16+
17+
> **注意:** 在深入 facades 之前,我们强烈建议你多了解一下 Laravel [IoC 容器](/docs/4.2/ioc)
18+
19+
<a name="explanation"></a>
20+
## 说明
21+
22+
在 Laravel 应用程序中, facade 是提供从容器中访问对象的类。`Facade` 类实现了该机制。Laravel的facades,和其他自定义的facades,都需要继承基类`Facade`
23+
24+
你的 facade 类只需要实现一个方法: `getFacadeAccesor``getFacadeAccessor` 方法的工作是定义如何从容器中取得对象。 Facades 基类构建了 `__callStatic()` 魔术方法来从 facade 延迟访问取得对象。
25+
26+
因此,当你使用facade调用,类似`Cache::get`,Laravel会从IoC容器取得Cache管理类并调用`get`方法。在技术层上说,Laravel Facades是一种便捷的语法使用Laravel IoC容器作为服务定位器.
27+
28+
<a name="practical-usage"></a>
29+
## 实际用例
30+
31+
在以下示例中,调用Laravel缓存系统, 咋一看该代码,可能会认为`get`静态方法是在`Cache`类执行。
32+
33+
$value = Cache::get('key');
34+
35+
然后,如果我们查看`Illuminate\Support\Facades\Cache`类, 你会发现该类没有任何`get`静态方法:
36+
37+
class Cache extends Facade {
38+
39+
/**
40+
* Get the registered name of the component.
41+
*
42+
* @return string
43+
*/
44+
protected static function getFacadeAccessor() { return 'cache'; }
45+
46+
}
47+
48+
`Cache`类继承`Facade`这个基类,并且定义了个`getFacadeAccessor()`方法。注意,该方法的工作是返回绑定到`IoC`的名字。
49+
50+
当用户引用任何在`Cache` facade 中的静态方法, Laravel 从 IoC 容器绑定中取得 `cache`,并且执行请求的对象方法(在该例子中为`get`)。
51+
52+
所以,我们 `Cache::get` 执行可以重写为:
53+
54+
$value = $app->make('cache')->get('key');
55+
56+
<a name="creating-facades"></a>
57+
## 创建Facades
58+
59+
要为自己的应用程序或者包创建一个facade是非常简单的。你只需要做三件事情:
60+
61+
- 一个 IoC 绑定。
62+
- 一个 facade 类。
63+
- 一个 facade 别名配置。
64+
65+
让我们来看个例子。这里,我们定义一个`PaymentGateway\Payment`类。
66+
67+
namespace PaymentGateway;
68+
69+
class Payment {
70+
71+
public function process()
72+
{
73+
//
74+
}
75+
76+
}
77+
78+
这个类可以放在`app/models`目录,或者其他任何Composer能够自动载入的位置。
79+
80+
我们需要能够在 IoC 容器中取得该类。所以,让我们增加一个绑定:
81+
82+
App::bind('payment', function()
83+
{
84+
return new \PaymentGateway\Payment;
85+
});
86+
87+
最好注册该绑定的位置是创建一个新的名为`PaymentServiceProvider`[服务提供器](/docs/4.2/ioc#service-providers),并且将该绑定加入到 `register` 方法。接下来你就可以配置 Laravel `app/config/app.php` 配置文件来加载该服务提供器。
88+
89+
接下来,我们就可以创建我们的 facade 类:
90+
91+
use Illuminate\Support\Facades\Facade;
92+
93+
class Payment extends Facade {
94+
95+
protected static function getFacadeAccessor() { return 'payment'; }
96+
97+
}
98+
99+
最后,如果你想,我们可以为我们 facade 设置一个别名到 `app/config/app.php` 配置文件里的 `aliases` 数组。现在,我们能够调用 Payment 类实例的 `process` 方法。
100+
101+
Payment::process();
102+
103+
### 关于自动载入别名一些注意点
104+
105+
`aliases`数组中的有些类接口可能是不可行的,因为[PHP不会尝试去自动载入未定义的类型约定](https://bugs.php.net/bug.php?id=39003)。假设`\ServiceWrapper\ApiTimeoutException`别名是`ApiTimeoutException`, 如果`catch(ApiTimeoutException $e)` 是在`\ServiceWrapper`命名空间之外使用,它将会永远不会被捕获到, 即使真的有一个异常被抛出。 一个类似的问题存在在那些进行类型约定的别名类上。唯一的变通方法是摒弃别名,在每个你想使用类型约定的文件的开始的地方使用`use`
106+
107+
<a name="mocking-facades"></a>
108+
## 模拟Facades
109+
110+
单元测试是 facades 工作的重要体现。事实上,可测试性是 facedes 存在的主要原因。要了解更多信息,查看文档[模拟 facades](/docs/4.2/testing#mocking-facades)部分。
111+
112+
<a name="facade-class-reference"></a>
113+
## Facade 类参考
114+
115+
下面你会找到所有的facade以及其包含的类。这是一个非常有用的工具,可以根据给定的facade快速定位到API文档。适用于[IoC 绑定](/docs/ioc) 的也同时给出了其key。
116+
117+
Facade | Class | IoC Binding
118+
------------- | ------------- | -------------
119+
App | [Illuminate\Foundation\Application](http://laravel.com/api/4.2/Illuminate/Foundation/Application.html) | `app`
120+
Artisan | [Illuminate\Console\Application](http://laravel.com/api/4.2/Illuminate/Console/Application.html) | `artisan`
121+
Auth | [Illuminate\Auth\AuthManager](http://laravel.com/api/4.2/Illuminate/Auth/AuthManager.html) | `auth`
122+
Auth (Instance) | [Illuminate\Auth\Guard](http://laravel.com/api/4.2/Illuminate/Auth/Guard.html) |
123+
Blade | [Illuminate\View\Compilers\BladeCompiler](http://laravel.com/api/4.2/Illuminate/View/Compilers/BladeCompiler.html) | `blade.compiler`
124+
Cache | [Illuminate\Cache\Repository](http://laravel.com/api/4.2/Illuminate/Cache/Repository.html) | `cache`
125+
Config | [Illuminate\Config\Repository](http://laravel.com/api/4.2/Illuminate/Config/Repository.html) | `config`
126+
Cookie | [Illuminate\Cookie\CookieJar](http://laravel.com/api/4.2/Illuminate/Cookie/CookieJar.html) | `cookie`
127+
Crypt | [Illuminate\Encryption\Encrypter](http://laravel.com/api/4.2/Illuminate/Encryption/Encrypter.html) | `encrypter`
128+
DB | [Illuminate\Database\DatabaseManager](http://laravel.com/api/4.2/Illuminate/Database/DatabaseManager.html) | `db`
129+
DB (Instance) | [Illuminate\Database\Connection](http://laravel.com/api/4.2/Illuminate/Database/Connection.html) |
130+
Event | [Illuminate\Events\Dispatcher](http://laravel.com/api/4.2/Illuminate/Events/Dispatcher.html) | `events`
131+
File | [Illuminate\Filesystem\Filesystem](http://laravel.com/api/4.2/Illuminate/Filesystem/Filesystem.html) | `files`
132+
Form | [Illuminate\Html\FormBuilder](http://laravel.com/api/4.2/Illuminate/Html/FormBuilder.html) | `form`
133+
Hash | [Illuminate\Hashing\HasherInterface](http://laravel.com/api/4.2/Illuminate/Hashing/HasherInterface.html) | `hash`
134+
HTML | [Illuminate\Html\HtmlBuilder](http://laravel.com/api/4.2/Illuminate/Html/HtmlBuilder.html) | `html`
135+
Input | [Illuminate\Http\Request](http://laravel.com/api/4.2/Illuminate/Http/Request.html) | `request`
136+
Lang | [Illuminate\Translation\Translator](http://laravel.com/api/4.2/Illuminate/Translation/Translator.html) | `translator`
137+
Log | [Illuminate\Log\Writer](http://laravel.com/api/4.2/Illuminate/Log/Writer.html) | `log`
138+
Mail | [Illuminate\Mail\Mailer](http://laravel.com/api/4.2/Illuminate/Mail/Mailer.html) | `mailer`
139+
Paginator | [Illuminate\Pagination\Factory](http://laravel.com/api/4.2/Illuminate/Pagination/Factory.html) | `paginator`
140+
Paginator (Instance) | [Illuminate\Pagination\Paginator](http://laravel.com/api/4.2/Illuminate/Pagination/Paginator.html) |
141+
Password | [Illuminate\Auth\Reminders\PasswordBroker](http://laravel.com/api/4.2/Illuminate/Auth/Reminders/PasswordBroker.html) | `auth.reminder`
142+
Queue | [Illuminate\Queue\QueueManager](http://laravel.com/api/4.2/Illuminate/Queue/QueueManager.html) | `queue`
143+
Queue (Instance) | [Illuminate\Queue\QueueInterface](http://laravel.com/api/4.2/Illuminate/Queue/QueueInterface.html) |
144+
Queue (Base Class) | [Illuminate\Queue\Queue](http://laravel.com/api/4.2/Illuminate/Queue/Queue.html) |
145+
Redirect | [Illuminate\Routing\Redirector](http://laravel.com/api/4.2/Illuminate/Routing/Redirector.html) | `redirect`
146+
Redis | [Illuminate\Redis\Database](http://laravel.com/api/4.2/Illuminate/Redis/Database.html) | `redis`
147+
Request | [Illuminate\Http\Request](http://laravel.com/api/4.2/Illuminate/Http/Request.html) | `request`
148+
Response | [Illuminate\Support\Facades\Response](http://laravel.com/api/4.2/Illuminate/Support/Facades/Response.html) |
149+
Route | [Illuminate\Routing\Router](http://laravel.com/api/4.2/Illuminate/Routing/Router.html) | `router`
150+
Schema | [Illuminate\Database\Schema\Blueprint](http://laravel.com/api/4.2/Illuminate/Database/Schema/Blueprint.html) |
151+
Session | [Illuminate\Session\SessionManager](http://laravel.com/api/4.2/Illuminate/Session/SessionManager.html) | `session`
152+
Session (Instance) | [Illuminate\Session\Store](http://laravel.com/api/4.2/Illuminate/Session/Store.html) |
153+
SSH | [Illuminate\Remote\RemoteManager](http://laravel.com/api/4.2/Illuminate/Remote/RemoteManager.html) | `remote`
154+
SSH (Instance) | [Illuminate\Remote\Connection](http://laravel.com/api/4.2/Illuminate/Remote/Connection.html) |
155+
URL | [Illuminate\Routing\UrlGenerator](http://laravel.com/api/4.2/Illuminate/Routing/UrlGenerator.html) | `url`
156+
Validator | [Illuminate\Validation\Factory](http://laravel.com/api/4.2/Illuminate/Validation/Factory.html) | `validator`
157+
Validator (Instance) | [Illuminate\Validation\Validator](http://laravel.com/api/4.2/Illuminate/Validation/Validator.html)
158+
View | [Illuminate\View\Factory](http://laravel.com/api/4.2/Illuminate/View/Factory.html) | `view`
159+
View (Instance) | [Illuminate\View\View](http://laravel.com/api/4.2/Illuminate/View/View.html) |
160+
161+
162+
163+
译者:mpandar(马胜盼)

0 commit comments

Comments
 (0)