Home  >  Article  >  Backend Development  >  Laravel支持什么缓存

Laravel支持什么缓存

PHPz
PHPzOriginal
2016-06-06 20:17:091688browse

Laravel支持什么缓存

Laravel支持什么缓存?

Laravel 为各种后端缓存提供丰富而统一的 API,而其配置信息位于 config/cache.php 文件中,你可以指定默认的缓存驱动程序.

Laravel 支持当前流行的后端缓存,例如 Memcached 和 Redis。

缓存配置文件还包含各种其他选项,这些选项都记录在文件中,因此请确保阅读这些选项。 默认情况下,Laravel 配置为使用 file 缓存驱动程序,它将序列化的缓存对象存储在文件系统中。 对于较大的应用程序,建议您使用更强大的驱动程序,例如 Memcached 或 Redis。 你甚至可以为同一个驱动程序配置多个缓存配置。

驱动的前提条件

数据库

当使用 database 缓存驱动时,你需要配置一个表来存放缓存数据,下面是构建缓存数据表结构的 Schema 声明示例:

Schema::create('cache', function ($table) {
    $table->string('key')->unique();
    $table->text('value');
    $table->integer('expiration');
});

{tip} 你也可以使用 Artisan 命令 php artisan cache:table 来生成合适的迁移。

Memcached

使用 Memcached 驱动需要安装 Memcached PECL 扩展包 。你可以把所有 Memcached 服务器都列在 config/cache.php 配置文件中:

'memcached' => [
    [
        'host' => '127.0.0.1',
        'port' => 11211,
        'weight' => 100
    ],
],

你可以将 host 选项设置为 UNIX 的 socket 路径。如果你这样配置了,记得 port 选项应该设置为 0:

'memcached' => [
    [
        'host' => '/var/run/memcached/memcached.sock',
        'port' => 0,
        'weight' => 100
    ],
],

Redis

在使用 Laravel 的 Redis 缓存之前,你需要通过 Composer 安装 predis/predis 扩展包 (~1.0) 或者使用 PECL 安装 PhpRedis PHP 拓展。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn