官方文档:https://learnku.com/docs/laravel/6.x/cache/5160#224e2c
Laravel 为各种后端缓存提供了丰富而统一的 API,其配置信息位于 config/cache.php 文件中。
缓存配置文件还包含各种其他选项,这些选项都记录在文件中,因此请确保阅读这些选项。
默认情况下,Laravel 配置为使用 file 缓存驱动,它将序列化的缓存对象存储在文件系统中。
特殊说明 使用数据库进行数据缓存:
数据库
当使用 database 缓存驱动时,你需要配置一个表来存放缓存数据。下面是构建缓存数据表结构的 Schema 声明示例:
Schema::create('cache', function ($table) { $table->string('key')->unique(); $table->text('value'); $table->integer('expiration'); });
提示:你也可以使用 Artisan 命令 php artisan cache:table 来生成合适的迁移。
一般情况也不会使用数据库当成缓存启动的。
config/cache.php
<?php use Illuminate\Support\Str; return [ /* |-------------------------------------------------------------------------- | Default Cache Store |-------------------------------------------------------------------------- | | This option controls the default cache connection that gets used while | using this caching library. This connection is used when another is | not explicitly specified when executing a given caching function. | | Supported: "apc", "array", "database", "file", | "memcached", "redis", "dynamodb" | */ 'default' => env('CACHE_DRIVER', 'file'), /* |-------------------------------------------------------------------------- | Cache Stores |-------------------------------------------------------------------------- | | Here you may define all of the cache "stores" for your application as | well as their drivers. You may even define multiple stores for the | same cache driver to group types of items stored in your caches. | */ 'stores' => [ 'apc' => [ 'driver' => 'apc', ], 'array' => [ 'driver' => 'array', ], 'database' => [ 'driver' => 'database', 'table' => 'cache', 'connection' => null, ], 'file' => [ 'driver' => 'file', 'path' => storage_path('framework/cache/data'), ], 'memcached' => [ 'driver' => 'memcached', 'persistent_id' => env('MEMCACHED_PERSISTENT_ID'), 'sasl' => [ env('MEMCACHED_USERNAME'), env('MEMCACHED_PASSWORD'), ], 'options' => [ // Memcached::OPT_CONNECT_TIMEOUT => 2000, ], 'servers' => [ [ 'host' => env('MEMCACHED_HOST', '127.0.0.1'), 'port' => env('MEMCACHED_PORT', 11211), 'weight' => 100, ], ], ], 'redis' => [ 'driver' => 'redis', 'connection' => 'session', //此模块与 config/database.php 中的redis 相关联 ], 'dynamodb' => [ 'driver' => 'dynamodb', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'table' => env('DYNAMODB_CACHE_TABLE', 'cache'), 'endpoint' => env('DYNAMODB_ENDPOINT'), ], ], /* |-------------------------------------------------------------------------- | Cache Key Prefix |-------------------------------------------------------------------------- | | When utilizing a RAM based store such as APC or Memcached, there might | be other applications utilizing the same cache. So, we'll specify a | value to get prefixed to all our keys so we can avoid collisions. | */ 'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'), ];
设置缓存
$flag = cache()->put('vvv','vvvv123'); use Illuminate\Support\Facades\Cache; Cache::put('key', 'value', 0); Cache::put('key', 'value', -5);
获取缓存
$flag =cache()->get('vvv'); use Illuminate\Support\Facades\Cache; $value = Cache::get('key'); $value = Cache::get('key', 'default'); //还可以放一个回调函数,从里面直接获取数据 $value = Cache::get('key', function () { return DB::table(...)->get(); });
删除缓存
$flag =cache()->forget('vvv');
访问多个缓存存储
使用 Cache Facade,你可以通过 store 方法来访问各种缓存存储。传入 store 方法的键应该对应 cache 配置信息文件中的 stores 配置数组中所列出的一个:
use Illuminate\Support\Facades\Cache; $value = Cache::store('file')->get('foo'); Cache::store('redis')->put('bar', 'baz', 600); // 10 分钟
检查缓存项是否存在
has 方法可以用于判断缓存项是否存在。如果值为 null,则该方法将会返回 false :
use Illuminate\Support\Facades\Cache; if (Cache::has('key')) { // }
递增与递减值
increment 和 decrement 方法可以用来调整缓存中整数项的值。这两个方法都可以传入第二个可选参数,这个参数用来指明要递增或递减的数量:
use Illuminate\Support\Facades\Cache; Cache::increment('key'); Cache::increment('key', $amount); Cache::decrement('key'); Cache::decrement('key', $amount);
flush 方法清空所有的缓存
Cache::flush();
注意:清空缓存的方法并不会考虑缓存前缀,会将缓存中的所有内容删除。因此在清除与其它应用程序共享的缓存时,请慎重考虑。
注册驱动 拓展新缓存
在 Laravel 注册一个自定义的缓存驱动,我们需要在 Cache 门面上使用 extend 方法。 对 Cache::extend 的调用可以在新的 Laravel 应用程序中自带的 App\Providers\AppServiceProvider 的 boot 方法中完成,或者你也可以自己创建服务提供者来存放扩展,只是不要忘记在 config/app.php 的 provider 的数组中注册服务提供者:
<?php namespace App\Providers; use App\Extensions\MongoStore; use Illuminate\Support\Facades\Cache; use Illuminate\Support\ServiceProvider; class CacheServiceProvider extends ServiceProvider { /** * 在容器中注册绑定 * * @return void */ public function register() { // } /** * 执行服务注册后的引导 * * @return void */ public function boot() { Cache::extend('mongo', function ($app) { return Cache::repository(new MongoStore); }); } }
传递给 extend 方法的第一个参数是驱动程序的名称。
该值对应 config/cache.php 配置文件中的 driver 选项。
第二个参数是返回 Illuminate\Cache\Repository 实例的闭包。
该闭包中被传入一个 $app 的实例, 也就是 服务容器 的一个实例。
一旦你的扩展被注册以后,只需要更新配置文件 config/cache.php 的 driver 选项作为自定义扩展名称即可。