Skip to content

Commit 7e55888

Browse files
authored
Implemented cache tags to prevent clearing all caches every reindex (#1104)
1 parent 1aa0a36 commit 7e55888

File tree

6 files changed

+9
-8
lines changed

6 files changed

+9
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"lcobucci/jwt": "^4.0|^5.3",
3131
"rapidez/blade-components": "^1.11",
3232
"rapidez/blade-directives": "^1.1",
33-
"rapidez/laravel-multi-cache": "^2.0",
33+
"rapidez/laravel-multi-cache": "^2.1",
3434
"rapidez/laravel-scout-elasticsearch": "^1.0",
3535
"tormjens/eventy": "^0.8"
3636
},

src/Commands/IndexCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rapidez\Core\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Cache;
67
use Rapidez\Core\Events\IndexAfterEvent;
78
use Rapidez\Core\Events\IndexBeforeEvent;
89
use Rapidez\Core\Facades\Rapidez;
@@ -28,7 +29,7 @@ public function handle()
2829
? Rapidez::getStores(explode(',', $this->option('store')))
2930
: Rapidez::getStores();
3031

31-
$this->call('cache:clear');
32+
Cache::driver('rapidez:multi')->tags(['attributes', 'swatches'])->flush();
3233

3334
IndexBeforeEvent::dispatch($this);
3435

src/Models/Attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function prefix(): CastsAttribute
4646

4747
public static function getCachedWhere(callable $callback): array
4848
{
49-
$attributes = Cache::store('rapidez:multi')->rememberForever('attributes.' . config('rapidez.store'), function () {
49+
$attributes = Cache::store('rapidez:multi')->tags('attributes')->rememberForever('attributes.' . config('rapidez.store'), function () {
5050
return self::all()->toArray();
5151
});
5252

src/Models/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function getValue(
8686
}
8787

8888
if ($options['cache'] ?? true) {
89-
$configCache = Cache::driver('rapidez:multi')->get('magento.config', []);
89+
$configCache = Cache::driver('rapidez:multi')->tags('config')->get('magento.config', []);
9090
$cacheKey = implode(
9191
'.',
9292
[
@@ -126,7 +126,7 @@ public static function getValue(
126126

127127
if (($options['cache'] ?? true) && isset($cacheKey)) {
128128
Arr::set($configCache, $cacheKey, $resultObject ? $result : false);
129-
Cache::driver('rapidez:multi')->set('magento.config', $configCache);
129+
Cache::driver('rapidez:multi')->tags('config')->set('magento.config', $configCache);
130130
}
131131

132132
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)