一、基本操作
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)、配置文件
<?phpuse 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);//設置