緩存系統(tǒng)
Laravel 為各種后端緩存提供了豐富而統(tǒng)一的 API,其配置信息位于 config/cache.php 文件中。默認情況下,Laravel 配置為使用 file 緩存驅(qū)動,它將序列化的緩存對象存儲在文件系統(tǒng)中陕习。
緩存配置
mysql:
Schema::create('cache', function ($table) {
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
});
file:
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache/data'),
],
memcache:
'memcached' => [
[
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 100
],
],
redis:
// cache.php
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
],
// database.php
'redis' => [
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
使用示例
// 獲取某個緩存
$value = Cache::get('key');
$value = Cache::get('key', 'default');
$value = Cache::get('key', function () {
return DB::table(...)->get();
});
// 訪問多個緩存存儲
$value = Cache::store('file')->get('foo');
Cache::store('redis')->put('bar', 'baz', $seconds);
// 檢查緩存是否存在
if (Cache::has('key')) {
}
// 遞增與遞減
Cache::increment('key');
Cache::increment('key', $amount);
Cache::decrement('key');
Cache::decrement('key', $amount);
// 獲取不存時則存儲緩存
$value = Cache::remember('users', $seconds, function () {
return DB::table('users')->get();
});
// 獲取數(shù)據(jù)或永久存儲緩存
$value = Cache::rememberForever('users', function () {
return DB::table('users')->get();
});
// 獲取數(shù)據(jù)并刪除緩存(不存在返回 null)
$value = Cache::pull('key');
// 存儲緩存數(shù)據(jù)(沒有過期時間則永久有效)
Cache::put('key', 'value', $seconds);
Cache::put('key', 'value');
Cache::put('key', 'value', now()->addMinutes(10));
// 僅存儲不存在的數(shù)據(jù)(true 和 fasle)
Cache::add('key', 'value', $seconds);
// 永久存儲緩存數(shù)據(jù)(必須通過 forget 才能刪除)
Cache::forever('key', 'value');
// 刪除緩存數(shù)據(jù)
Cache::forget('key');
// 通過零或負值來刪除數(shù)據(jù)
Cache::put('key', 'value', 0);
Cache::put('key', 'value', -5);
// 清空所有的緩存
Cache::flush();
// 原子鎖(僅限使用 memcache、redis 等緩存驅(qū)動)
// 原子鎖允許對分布式鎖進行操作而不必擔心競爭條件,即使用原子鎖來確保在一臺服務器上每次只有一個遠程任務在執(zhí)行。
$lock = Cache::lock('foo', 10);
if ($lock->get()) {
// 獲取鎖定10秒...
$lock->release();
}
Cache::lock('foo')->get(function () {
// 獲取無限期鎖并自動釋放...
});
// 在指定的時間限制內(nèi)無法獲取鎖則拋出異常
use Illuminate\Contracts\Cache\LockTimeoutException;
$lock = Cache::lock('foo', 10);
try {
$lock->block(5);
// 等待最多5秒后獲取的鎖...
} catch (LockTimeoutException $e) {
// 無法獲取鎖...
} finally {
optional($lock)->release();
}
Cache::lock('foo', 10)->block(5, function () {
// 等待最多5秒后獲取的鎖...
});
// 管理跨進程鎖
// 控制器里
$podcast = Podcast::find($id);
$lock = Cache::lock('foo', 120);
if ($result = $lock->get()) {
ProcessPodcast::dispatch($podcast, $lock->owner());
}
// 隊列里
Cache::restoreLock('foo', $this->owner)->release();
// 無視當前鎖的所有者的情況下釋放鎖
Cache::lock('foo')->forceRelease();
// Cache 輔助函數(shù)
$value = cache('key');
cache(['key' => 'value'], $seconds);
cache(['key' => 'value'], now()->addMinutes(10));
cache()->remember('users', $seconds, function () {
return DB::table('users')->get();
});
緩存標記
緩存標簽允許你給相關的緩存標簽項打上同一個標簽以便后續(xù)可以清除這些緩存值窍侧。
// 存儲被打上標簽的緩存數(shù)據(jù)
Cache::tags(['people', 'artists'])->put('John', $john, $seconds);
Cache::tags(['people', 'authors'])->put('Anne', $anne, $seconds);
// 訪問被打上標簽的緩存數(shù)據(jù)
$john = Cache::tags(['people', 'artists'])->get('John');
$anne = Cache::tags(['people', 'authors'])->get('Anne');
// 移除帶有標簽的緩存數(shù)據(jù)
Cache::tags(['people', 'authors'])->flush();
事件系統(tǒng)
Laravel 的事件提供了一個簡單的觀察者實現(xiàn),允許你在應用中訂閱和監(jiān)聽各種發(fā)生的事件转绷。事件類通常放在 app/Events 目錄下伟件,而這些事件類的監(jiān)聽器則放在 app/Listeners 目錄下。如果在你的應用中你沒有看到這些目錄暇咆,不用擔心锋爪,它們會在你使用 Artisan 控制臺命令生成事件與監(jiān)聽器的時候自動創(chuàng)建。