PHP laravel緩存cache機(jī)制詳解
目錄
- 一、訪問(wèn)多個(gè)緩存存儲(chǔ)
- 二、從緩存中獲取數(shù)據(jù)
- 1.獲取數(shù)據(jù)并設(shè)置默認(rèn)值
- 2.檢查緩存項(xiàng)是否存在
- 3.數(shù)值增加/減少
- 4.獲取 & 存儲(chǔ)
- 5.獲取 & 刪除
- 三、緩存中存儲(chǔ)數(shù)據(jù)
- 1.獲取存儲(chǔ)數(shù)據(jù)
- 2.緩存不存在時(shí)存儲(chǔ)數(shù)據(jù)
- 3.永久存儲(chǔ)數(shù)據(jù)
- 四、從緩存中移除數(shù)據(jù)
Laravel中的cache為我們提供了三種緩存機(jī)制。
Redis,memcache,以及框架的文件緩存。
這里主要看的是cache中的文件緩存。
一、訪問(wèn)多個(gè)緩存存儲(chǔ)
使用 Cache 門面,你可以使用 store 方法訪問(wèn)不同的緩存存儲(chǔ)器,傳入 store 方法的鍵就是 cache 配置文件中 stores 配置數(shù)組里列出的相應(yīng)的存儲(chǔ)器:
$value = Cache::store("file")->get("foo");
Cache::store("redis")->put("bar", "baz", 600); // 10分鐘
二、從緩存中獲取數(shù)據(jù)
1.獲取數(shù)據(jù)并設(shè)置默認(rèn)值
(1):正常取值
$value = Cache::get("key");
(2):如果不存在,附默認(rèn)值
$value = Cache::get("key", "default");
(3):使用閉包操作,附默認(rèn)值
$value = Cache::get("key", function() {
return DB::table(...)->get();
});
2.檢查緩存項(xiàng)是否存在
has 方法用于判斷緩存項(xiàng)是否存在,如果值為 null 或 false 該方法會(huì)返回 false:
if (Cache::has("key")) {
//
}
3.數(shù)值增加/減少
increment 和 decrement 方法可用于調(diào)整緩存中的整型數(shù)值。這兩個(gè)方法都可以接收第二個(gè)參數(shù)來(lái)指明緩存項(xiàng)數(shù)值增加和減少的數(shù)目:
Cache::increment("key");
Cache::increment("key", $amount);
Cache::decrement("key");
Cache::decrement("key", $amount);
4.獲取 & 存儲(chǔ)
有時(shí)候你可能想要獲取緩存項(xiàng),但如果請(qǐng)求的緩存項(xiàng)不存在時(shí)給它存儲(chǔ)一個(gè)默認(rèn)值。例如,你可能想要從緩存中獲取所有用戶,或者如果它們不存在的話,從數(shù)據(jù)庫(kù)獲取它們并將其添加到緩存中,你可以通過(guò)使用 Cache::remember 方法實(shí)現(xiàn):
$value = Cache::remember("users", $seconds, function() {
return DB::table("users")->get();
});
如果緩存項(xiàng)不存在,傳遞給 remember 方法的閉包被執(zhí)行并且將結(jié)果存放到緩存中。
你還可以使用 rememberForever 方法從緩存中獲取數(shù)據(jù)或者將其永久存儲(chǔ)起來(lái):
$value = Cache::rememberForever("users", function() {
return DB::table("users")->get();
});
5.獲取 & 刪除
如果你需要從緩存中獲取緩存項(xiàng)然后刪除,你可以使用 pull 方法,和 get 方法一樣,如果緩存項(xiàng)不存在的話返回 null:
$value = Cache::pull("key");
三、緩存中存儲(chǔ)數(shù)據(jù)
1.獲取存儲(chǔ)數(shù)據(jù)
你可以使用 Cache 門面上的 put 方法在緩存中存儲(chǔ)數(shù)據(jù)。當(dāng)你在緩存中存儲(chǔ)數(shù)據(jù)的時(shí)候,需要指定數(shù)據(jù)被緩存的時(shí)間(秒數(shù)):
Cache::put("key", "value", $seconds);
如果沒(méi)有傳遞緩存時(shí)間到 put 方法,則緩存項(xiàng)永久有效:
Cache::put("key", "value");
除了傳遞緩存項(xiàng)失效時(shí)間,你還可以傳遞一個(gè)代表緩存項(xiàng)有效時(shí)間的 PHP Datetime 實(shí)例:
$expiresAt = Carbon::now()->addMinutes(10);
Cache::put("key", "value", $expiresAt);
2.緩存不存在時(shí)存儲(chǔ)數(shù)據(jù)
add 方法只會(huì)在緩存項(xiàng)不存在的情況下添加數(shù)據(jù)到緩存,如果數(shù)據(jù)被成功添加到緩存返回 true,否則,返回 false:
Cache::add("key", "value", $seconds);
3.永久存儲(chǔ)數(shù)據(jù)
forever 方法用于持久化存儲(chǔ)數(shù)據(jù)到緩存,這些值必須通過(guò) forget 方法手動(dòng)從緩存中移除:
Cache::forever("key", "value");
四、從緩存中移除數(shù)據(jù)
可以使用 Cache 門面上的 forget 方法從緩存中移除緩存項(xiàng)數(shù)據(jù):
Cache::forget("key");
還可以通過(guò)設(shè)置緩存有效期為 0 或負(fù)數(shù)來(lái)移除緩存項(xiàng):
Cache::put("key", "value", 0);
Cache::put("key", "value", -5);
如果要清除所有緩存,可以通過(guò) flush 方法:
Cache::flush();
以上基本上就是laravel框架對(duì)文件緩存的操作方法
到此這篇關(guān)于PHP laravel緩存cache機(jī)制詳解的文章就介紹到這了,更多相關(guān)PHP laravel緩存內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

網(wǎng)公網(wǎng)安備