Skip to content

Commit cc63322

Browse files
committed
Implemented cache tags to prevent clearing all caches every reindex
1 parent cb9e74b commit cc63322

File tree

6 files changed

+10
-9
lines changed

6 files changed

+10
-9
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"minimum-stability": "dev",
1717
"prefer-stable": true,
1818
"require": {
19-
"php": "^8.1|^8.2|^8.3",
20-
"rapidez/laravel-multi-cache": "^1.0",
19+
"php": "^8.2|^8.3",
20+
"rapidez/laravel-multi-cache": "^2.1",
2121
"blade-ui-kit/blade-heroicons": "^2.4",
2222
"mailerlite/laravel-elasticsearch": "^11.1",
2323
"illuminate/database": "^10.0|^11.0",

src/Commands/IndexProductsCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Cache;
89
use Rapidez\Core\Events\IndexAfterEvent;
910
use Rapidez\Core\Events\IndexBeforeEvent;
1011
use Rapidez\Core\Facades\Rapidez;
@@ -22,7 +23,7 @@ class IndexProductsCommand extends ElasticsearchIndexCommand
2223

2324
public function handle()
2425
{
25-
$this->call('cache:clear');
26+
Cache::driver('rapidez:multi')->tags(['attributes', 'swatches'])->flush();
2627

2728
IndexBeforeEvent::dispatch($this);
2829

src/Models/Attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function filter(): CastsAttribute
2727

2828
public static function getCachedWhere(callable $callback): array
2929
{
30-
$attributes = Cache::store('rapidez:multi')->rememberForever('attributes.' . config('rapidez.store'), function () {
30+
$attributes = Cache::store('rapidez:multi')->tags('attributes')->rememberForever('attributes.' . config('rapidez.store'), function () {
3131
return self::all()->toArray();
3232
});
3333

src/Models/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function getValue(
8282
};
8383

8484
if ($options['cache'] ?? true) {
85-
$configCache = Cache::driver('rapidez:multi')->get('magento.config', []);
85+
$configCache = Cache::driver('rapidez:multi')->tags('config')->get('magento.config', []);
8686
$cacheKey = implode(
8787
'.',
8888
[
@@ -122,7 +122,7 @@ public static function getValue(
122122

123123
if ($options['cache'] ?? true) {
124124
Arr::set($configCache, $cacheKey, $resultObject ? $result : false);
125-
Cache::driver('rapidez:multi')->set('magento.config', $configCache);
125+
Cache::driver('rapidez:multi')->tags('config')->set('magento.config', $configCache);
126126
}
127127

128128
return (bool) $options['decrypt'] && is_string($result) ? static::decrypt($result) : $result;

src/Models/OptionSwatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function getCachedSwatchValues(): array
2222
return $attribute['text_swatch'] || $attribute['visual_swatch'];
2323
}), 'id', 'code');
2424

25-
return Cache::rememberForever('swatchvalues', function () use ($swatchAttributes) {
25+
return Cache::store('rapidez:multi')->tags('swatches')->rememberForever('swatchvalues', function () use ($swatchAttributes) {
2626
return self::select('eav_attribute.attribute_code')
2727
->selectRaw('JSON_OBJECTAGG(`eav_attribute_option_value`.`option_id`, JSON_OBJECT(
2828
"label", COALESCE(`eav_attribute_option_value_store`.`value`, `eav_attribute_option_value`.`value`),

src/Models/OptionValue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class OptionValue extends Model
1313
public static function getCachedByOptionId(int $optionId): string
1414
{
1515
$cacheKey = 'optionvalues.' . config('rapidez.store');
16-
$cache = Cache::store('rapidez:multi')->get($cacheKey, []);
16+
$cache = Cache::store('rapidez:multi')->tags('attributes')->get($cacheKey, []);
1717

1818
if (! isset($cache[$optionId])) {
1919
$cache[$optionId] = html_entity_decode(self::where('option_id', $optionId)
2020
->whereIn('store_id', [config('rapidez.store'), 0])
2121
->orderByDesc('store_id')
2222
->first('value')
2323
->value ?? false);
24-
Cache::store('rapidez:multi')->forever($cacheKey, $cache);
24+
Cache::store('rapidez:multi')->tags('attributes')->forever($cacheKey, $cache);
2525
}
2626

2727
return $cache[$optionId];

0 commit comments

Comments
 (0)