I just started using redis to cache data and run queries. I have two functions, one for fetching the data, and another one for filtering the cached data. This is how my function for fetching data looks:
if (Auth::user()->access_level == 'Admin' || Auth::user()->access_level == 'Donor') {
$clients_number = Cache::remember('all_clients_number', 21600, function () {
return ClientPerformance::whereNotNull('actual_clients')->get();
});
$all_clients_number = Cache::remember('all_clients_sum', 21600, function () use ($clients_number) {
return $clients_number->sum('actual_clients');
});
} else if(Auth::user()->access_level == 'Partner') {
$clients_number = Cache::remember('all_partner_clients_number', 21600, function () {
return ClientPerformance::whereNotNull('actual_clients')
->where('partner_id', Auth::user()->partner_id)
->get();
});
$all_clients_number = Cache::remember('all_partner_clients_sum', 21600, function () use ($clients_number) {
return $clients_number->sum('actual_clients');
});
}
I have confirmed that my redis service is running and i changed the maximum memory limit in my php.ini file to be 4096.
This is how my function to filter the data looks like:
$selected_counties = $request->counties;
if (Auth::user()->access_level == 'Admin' || Auth::user()->access_level == 'Donor') {
$all_clients_number = Cache::remember('all_clients_number', 21600, function () {
return ClientPerformance::whereNotNull('actual_clients');
});
} else if(Auth::user()->access_level == 'Partner') {
$all_clients_number = Cache::remember('all_partner_clients_number', 21600, function () {
return ClientPerformance::whereNotNull('actual_clients');
});
}
if (!empty($selected_counties)) {
$all_clients_number = $all_clients_number->where('county_id', $selected_counties);
}
$data["all_clients_number"] = $all_clients_number->sum('actual_clients');
Is it good practice to access the cached data from another function like this Cache::remember(key)
and what could be the reason why i cant write anything to redis despite the fact that the data im caching is not that heavy.
Any advise or links to places i can read how to effectively access cached redit data will be appreciated. Thanks.
There are some workaround which can solve the issue this:
CONFIG SET maxmemory 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.