Cache 缓存

To extend the Laravel cache facility, we will use the extend method on the CacheManager, which is used to bind a custom driver resolver to the manager, and is common across all manager classes. For example, to register a new cache driver named "mongo", we would do the following:

要扩展 Laravel 的缓存机制,我们将使用 CacheManager 里的 extend 方法来绑定我们自定义的缓存驱动。扩展其他的管理类也是类似的。比如,我们想注册一个新的缓存驱动,名叫 “mongo”,代码可以这样写:

Cache::extend('mongo', function($app)
{
    // Return Illuminate\Cache\Repository instance...
});

The first argument passed to the extend method is the name of the driver. This will correspond to your driver option in the app/config/cache.php configuration file. The second argument is a Closure that should return an Illuminate\Cache\Repository instance. The Closure will be passed an $app instance, which is an instance of Illuminate\Foundation\Application and an IoC container.

extend 方法的第一个参数是你要定义的驱动的名字。该名字对应着 app/config/cache.php 配置文件中的 driver 项。第二个参数是一个匿名函数(闭包),该匿名函数有一个 $app 参数是 Illuminate\Foundation\Application 的实例也是一个 IoC 容器,该匿名函数要返回一个 Illuminate\Cache\Repository 的实例。

To create our custom cache driver, we first need to implement the Illuminate\Cache\StoreInterface contract. So, our MongoDB cache implementation would look something like this:

要创建我们自己的缓存驱动,首先要实现 Illuminate\Cache\StoreInterface 接口。所以我们用 MongoDB 来实现的缓存驱动就可能看上去是这样:

class MongoStore implements Illuminate\Cache\StoreInterface {
    public function get($key) {}
    public function put($key, $value, $minutes) {}
    public function increment($key, $value = 1) {}
    public function decrement($key, $value = 1) {}
    public function forever($key, $value) {}
    public function forget($key) {}
    public function flush() {}
}

We just need to implement each of these methods using a MongoDB connection. Once our implementation is complete, we can finish our custom driver registeration:

我们只需使用 MongoDB 链接来实现上面的每一个方法即可。一旦实现完毕,就可以照下面这样完成该驱动的注册:

use Illuminate\Cache\Repository;
Cache::extend('mongo', function($app)
{
    return new Repository(new MongoStore);
}

As you can see in the example above, you may use the base Illuminate\Cache\Repository when creating custom cache drivers. These is typically no need to create your own repository class.

你可以像上面的例子那样来创建 Illuminate\Cache\Repository 的实例。也就是说通常你不需要创建你自己的仓库类(Repository)。

If you're wondering where to put your custom cache driver code, consider making it available on Packagist! Or, you could create an Extensions namespace within your application's primary folder. For example, if the application is named Snappy, you could place the cache extension in app/Snappy/Extensions/MongoStore.php. However, keep in mind that Laravel doesn not have a rigid application structure and you are free to organize your application according to your preferences.

如果你不知道要把自定义的缓存驱动代码放到哪儿,可以考虑放到 Packagist 里!或者你也可以在你应用的主目录下创建一个 Extensions 目录。比如,你的应用叫做 Snappy ,你可以将缓存扩展代码放到 app/Snappy/Extensions/MongoStore.php 。不过请记住 Laravel 没有对应用程序的结构做硬性规定,所以你可以按任意你喜欢的方式组织你的代码。

Where To Extend 在哪儿调用Extend方法?

If you're ever wondering where to put a piece of code, always consider a service provider. As we've discussed, using a service provider to organize framework extensions is a great way to organize your code.

如果你还发愁在哪儿放注册代码,先考虑放到服务提供者里吧。我们之前就讲过,使用服务提供者是一种非常棒的管理你应用代码的途径。

results matching ""

    No results matching ""