如何在laravel中存储现有缓存集合和更新缓存

问题描述 投票:0回答:1
return Cache::remember('districts.all',60*60, function (){
    return District::all();
});

缓存集合是

[
 {
    "id": 1,
    "title": "Dhaka"
 },
 {
    "id": 2,
    "title": "Rajshahi"
 },
 {
    "id": 3,
    "title": "Barishal"
 }
]

现在如何在zone.all缓存中添加新的

 "id":4 ,
 "title":"New District"

以及如何更新此缓存

 "id":3,
 "title":"Khulna"
laravel caching laravel-5.5 laravel-cache
1个回答
0
投票

Laravel没有直接更新缓存的特殊功能,但您仍然可以清除districts.all并使用新值重新定义它:

Cache::forget('districts.all');

$district = new District();
$district->id = 4;
$district->name = "New District";

Cache::remember('districts.all', 60*60, function () use ($district) {
    return District::all()->push($district);
});
© www.soinside.com 2019 - 2024. All rights reserved.