Skip to content
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

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/zeus-bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,15 @@
'show_presets' => false,

'allow_design' => false,

/**
* since `collections` have many types, we cannot lazy load them
* but we cache them for a while to get better performance
* the key is: dataSource_*_response_md5
*
* here you can set the duration of the cache
*/
'cache' => [
'collection_values' => 30, // on seconds
],
];
31 changes: 20 additions & 11 deletions src/Fields/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use LaraZeus\Bolt\Facades\Bolt;
use LaraZeus\Bolt\Models\Field;
use LaraZeus\Bolt\Models\FieldResponse;
use Illuminate\Support\Facades\Cache;
use LaraZeus\Bolt\Models\Response;
use LaraZeus\BoltPro\Models\Field as FieldPreset;

Expand Down Expand Up @@ -165,21 +166,29 @@ public function getCollectionsValuesForResponse(Field $field, FieldResponse $res

$response = Arr::wrap($response);

// to not braking old dataSource structure
if ((int) $field->options['dataSource'] !== 0) {
$response = BoltPlugin::getModel('Collection')::query()
->find($field->options['dataSource'])
?->values
->whereIn('itemKey', $response)
->pluck('itemValue')
->join(', ') ?? '';
} else {
$dataSource = (int) $field->options['dataSource'] ?? $field->options['dataSource'];
$cacheKey = 'dataSource_' . $dataSource . '_response_' . md5(serialize($response));
atmonshi marked this conversation as resolved.
Show resolved Hide resolved

$response = Cache::remember($cacheKey, config('zeus-bolt.cache.collection_values') , function () use ($field, $response, $dataSource) {

// Handle case when dataSource is not zero (new structure)
if ($dataSource !== 0) {
return BoltPlugin::getModel('Collection')::query()
->find($dataSource)
?->values
->whereIn('itemKey', $response)
->pluck('itemValue')
->join(', ') ?? '';
}

// Handle case when dataSource is zero (old structure)
$dataSourceClass = new $field->options['dataSource'];
$response = $dataSourceClass->getQuery()

return $dataSourceClass->getQuery()
->whereIn($dataSourceClass->getKeysUsing(), $response)
->pluck($dataSourceClass->getValuesUsing())
->join(', ');
}
});

return (is_array($response)) ? implode(', ', $response) : $response;
}
Expand Down