-
-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat] : Implement caching mechanism for collection queries #327
Conversation
PR Summary
|
Thank you @mohaphez, nice work. |
Thank you for your response @atmonshi . Do you have any thoughts on the code below? This code allows us to use caching and remove duplicate queries. It also enables us to flush the cache with a specific key. The downside might be the use of Do you have any suggestions? public function getCollectionsValuesForResponse(Field $field, FieldResponse $resp): string
{
$response = $resp->response;
if (blank($response)) {
return '';
}
if (Bolt::isJson($response)) {
$response = json_decode($response);
}
// Ensure response is an array
$response = Arr::wrap($response);
$dataSource = (int)$field->options['dataSource'];
$cacheKey = 'dataSource_' . $dataSource . '_response';
// Handle default model: `Collection`
if ($dataSource !== 0) {
$collection = Cache::remember(
$cacheKey,
config('zeus-bolt.cache.collection_values'),
fn() => BoltPlugin::getModel('Collection')::find($dataSource)
);
return $collection?->values
->whereIn('itemKey', $response)
->pluck('itemValue')
->join(', ') ?? '';
}
// Handle custom model class as dataSource
if (class_exists($field->options['dataSource'])) {
$dataSourceClass = app($field->options['dataSource']);
$collection = Cache::remember(
$cacheKey,
config('zeus-bolt.cache.collection_values'),
fn() => $dataSourceClass->getQuery()->get()
);
return $collection->whereIn($dataSourceClass->getKeysUsing(), $response)
->pluck($dataSourceClass->getValuesUsing())
->join(', ');
}
return is_array($response) ? implode(', ', $response) : (string)$response;
} |
I’ve tested the latest changes, and everything works perfectly. If you’re ready to merge, please feel free to proceed. Thanks again for all your hard work! |
I have some collections with over a 100 record :) so this will be very bad. will marge later today. thank you. |
Implemented caching for collection queries to reduce redundant database calls and improve performance.
Cached responses for 30 seconds based on unique query parameters.