保存对象到缓存中
Cache::put("key", "value", $minutes);
使用 Carbon 对象配置缓存过期时间
$expiresAt = Carbon::now()->addMinutes(10);
Cache::put("key", "value", $expiresAt);
若是对象不存在,则将其存入缓存中
Cache::add("key", "value", $minutes);
当对象确实被加入缓存时,使用 add 方法将会返回 true 否则会返回 false 。
确认对象是否存在
if (Cache::has("key"))
{
//
}
从缓存中取得对象
$value = Cache::get("key");
取得对象或是返回默认值
$value = Cache::get("key", "default");
$value = Cache::get("key", function() { return "default"; });
永久保存对象到缓存中
Cache::forever("key", "value");
有时候您会希望从缓存中取得对象,而当此对象不存在时会保存一个默认值,您可以使用 Cache::remember 方法:
$value = Cache::remember("users", $minutes, function()
{
return DB::table("users")->get();
});
您也可以结合 remember 和 forever 方法:
$value = Cache::rememberForever("users", function()
{
return DB::table("users")->get();
});
请注意所有保存在缓存中的对象皆会被序列化,所以您可以任意保存各种类型的数据。
从缓存拉出对象
如果您需要从缓存中取得对象后将它删除,您可以使用 pull 方法:
$value = Cache::pull("key");
从缓存中删除对象
Cache::forget("key");
获取特定的缓存存储
当使用多种缓存存储时,你可以通过 store 方法来访问它们:
$value = Cache::store("foo")->get("key");