💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 1、配置 Lumen为不同的缓存系统提供了统一的API。缓存配置项位于env文件。在该文件中你可以指定在应用中默认使用哪个缓存驱动。Lumen目前支持流行的缓存后端如[Memcached](http://memcached.org/)和[Redis](http://redis.io/)等。对于大型应用,推荐使用内存缓存如[Memcached](http://laravelacademy.org/tags/memcached "View all posts in Memcached")或APC。 ### 1.1 缓存预备知识 **数据库** 使用`database`缓存驱动时,你需要设置一张表包含缓存缓存项。下面是该表的`Schema`声明: ~~~ Schema::create('cache', function($table) { $table->string('key')->unique(); $table->text('value'); $table->integer('expiration'); }); ~~~ **Memcached** 使用Memcached缓存要求安装了[Memcached PECL 包](http://pecl.php.net/package/memcached),即PHP Memcached扩展。[Memcached::addServer](http://php.net/manual/en/memcached.addserver.php)默认配置使用TCP/IP协议。 **[Redis](http://laravelacademy.org/tags/redis "View all posts in Redis")** 在Lumen中使用Redis缓存之前,你需要通过Composer安装`predis/predis`包(~1.0)和`illuminate/redis`包(~5.1)。 ## 2、缓存使用 ### 2.1 获取缓存实例 `Illuminate\Contracts\Cache\Factory`和`Illuminate\Contracts\Cache\Repository`[契约](http://laravelacademy.org/post/95.html)提供了访问Laravel的缓存服务的方法。`Factory`契约提供了所有访问应用定义的缓存驱动的方法。`Repository`契约通常是应用中`cache`配置文件中指定的默认缓存驱动的一个实现。 然而,你还可以使用`Cache`[门面](http://laravelacademy.org/post/97.html),这也是我们在整个文档中使用的方式,`Cache`门面提供了简单方便的方式对底层Lumen缓存契约实现进行访问。 例如,让我们在控制器中导入`Cache`门面: ~~~ <?php namespace App\Http\Controllers; use Cache; class UserController extends Controller{ /** * 显示应用所有用户列表 * * @return Response */ public function index() { $value = Cache::get('key'); // } } ~~~ **访问多个缓存存储** 使用`Cache`门面,你可以使用`store`方法访问不同的缓存存储器,传入`store`方法的键就是cache配置文件中`stores`配置数组里列出的相应的存储器: ~~~ $value = Cache::store('file')->get('foo'); Cache::store('redis')->put('bar', 'baz', 10); ~~~ ### 2.2 从缓存中获取数据 `Cache`门面的`get`方法用于从缓存中获取缓存项,如果缓存项不存在,返回null。如果需要的话你可以传递第二个参数到`get`方法指定缓存项不存在时返回的自定义默认值: ~~~ $value = Cache::get('key'); $value = Cache::get('key', 'default'); ~~~ 你甚至可以传递一个闭包作为默认值,如果缓存项不存在的话闭包的结果将会被返回。传递闭包允许你可以从数据库或其它外部服务获取默认值: ~~~ $value = Cache::get('key', function() { return DB::table(...)->get(); }); ~~~ **检查缓存项是否存在** `has`方法用于判断缓存项是否存在: ~~~ if (Cache::has('key')) { // } ~~~ **数值增加/减少** `increment`和`decrement`方法可用于调整缓存中的整型数值。这两个方法都可以接收第二个参数来指明缓存项数值增加和减少的数目: ~~~ Cache::increment('key'); Cache::increment('key', $amount); Cache::decrement('key'); Cache::decrement('key', $amount); ~~~ **获取或更新** 有时候你可能想要获取缓存项,但如果请求的缓存项不存在时给它存储一个默认值。例如,你可能想要从缓存中获取所有用户,或者如果它们不存在的话,从数据库获取它们并将其添加到缓存中,你可以通过使用`Cache::remember`方法实现: ~~~ $value = Cache::remember('users', $minutes, function() { return DB::table('users')->get();}); ~~~ 如果缓存项不存在,传递给`remember`方法的闭包被执行并且将结果存放到缓存中。 你还可以联合`remember`和`forever`方法: ~~~ $value = Cache::rememberForever('users', function() { return DB::table('users')->get(); }); ~~~ **获取并删除** 如果你需要从缓存中获取缓存项然后删除,你可以使用`pull`方法,和`get`方法一样,如果缓存项不存在的话返回null: ~~~ $value = Cache::pull('key'); ~~~ ### 2.3 存储缓存项到缓存 你可以使用`Cache` 门面上的`put`方法在缓存中存储缓存项。当你在缓存中存储缓存项的时候,你需要指定数据被缓存的时间(分钟数): ~~~ Cache::put('key', 'value', $minutes); ~~~ 除了传递缓存项失效时间,你还可以传递一个代表缓存项有效时间的PHP `Datetime`实例: ~~~ $expiresAt = Carbon::now()->addMinutes(10); Cache::put('key', 'value', $expiresAt); ~~~ `add`方法只会在缓存项不存在的情况下添加缓存项到缓存,如果缓存项被添加到缓存返回`true`,否则,返回`false`: ~~~ Cache::add('key', 'value', $minutes); ~~~ `forever`方法用于持久化存储缓存项到缓存,这些值必须通过`forget`方法手动从缓存中移除: ~~~ Cache::forever('key', 'value'); ~~~ ### 2.4 从缓存中移除数据 你可以使用`Cache`门面上的`forget`方法从缓存中移除缓存项: ~~~ Cache::forget('key'); ~~~