一、基本操作
Cache::put() 创建缓存(键,值,有效期(单位是秒))
Cache::get() 获取缓存
Cache::add() 只会在缓存项不存在的情况下添加数据到缓存,如果数据被成功返回 true,否则,返回 false
Cache::pull() 从缓存中获取缓存项然后删除,使用 pull 方法,如果缓存项不存在的话返回 null
Cache::forever() 用于持久化存储数据到缓存,这些值必须通过 forget 方法手动从缓存中移除
Cache::forget() 从缓存中移除缓存项数据
Cache::has() 用于判断缓存项是否存在,如果值为 null 或 false 该方法会返回 false
二、基本配置
laravel框架的缓存配置在config/cache.php文件中
1)、配置文件
<?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',
'serialize' => false,
],
'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' => 'cache',
],
'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'),
];
2)、缓存驱动
file - 将 Session 保存在 framework/cache/data 中。
cookie - Session 保存在安全加密的 Cookie 中。
database - Session 保存在关系型数据库中。
memcached / redis - Sessions 保存在其中一个快速且基于缓存的存储系统中。
array - Sessions 保存在 PHP 数组中,不会被持久化。
三、缓存操作
1)、设置缓存
Cache::put('key', 'value', $minutes);
//将不存在于缓存中的数据放入缓存中,如果存放成功返回 true ,否则返回 false
Cache::add('key', 'value', $minutes);
//数据永久存入缓存
Cache::forever('key', 'value');
//获取users缓存,如果不存在,执行第三个参数,将返回值存入缓存
$value = Cache::remember('userinfo', $minutes, function () {
return DB::table('userinfo')->get();
});
//获取users缓存,如果不存在,执行第三个参数,将返回值存入缓存并永久储存
$value = Cache::rememberForever('userinfo', function() {
return DB::table('userinfo')->get();
});
2)、获取缓存
$value = Cache::get('key')
//传递第二个参数,用来指定如果查找的数据不存在时,你希望返回的默认值
$value = Cache::get('key', 'default');
//第二个参数传递 Closure 作为默认值。如果指定的数据不存在于缓存中,将返回 Closure 的结果
$value = Cache::get('key', function () {
return DB::table(...)->get();
});
3)、判断缓存是否存在
//如果值为 null 或 不存在返回false
Cache::has('key')
4)、递增递减
Cache::increment('key');
Cache::increment('key', $num);
Cache::decrement('key');
Cache::decrement('key', $num);
5)、删除
//从缓存中获取到数据之后再删除它,如果缓存中不存在该数据, 则返回 null
$value = Cache::pull('key');
//删除缓存
Cache::forget('key');
//清空缓存
Cache::flush();
6)、使用多种缓存
$value = Cache::store('file')->get('foo');//获取
Cache::store('redis')->put('bar', 'baz', 10);//设置