Skip to content

Commit c19493b

Browse files
authored
Implemented cache tags to prevent clearing all caches every reindex (#1103)
1 parent 4b1a3c4 commit c19493b

File tree

6 files changed

+10
-9
lines changed

6 files changed

+10
-9
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ protected function prefix(): CastsAttribute
4747

4848
public static function getCached()
4949
{
50-
return Cache::rememberForever('eav_attributes', function () {
50+
return Cache::store('rapidez:multi')->tags('attributes')->rememberForever('eav_attributes', function () {
5151
return DB::table('eav_attribute')->get()->keyBy('attribute_code');
5252
});
5353
}
5454

5555
public static function getCachedWhere(callable $callback): array
5656
{
57-
$attributes = Cache::store('rapidez:multi')->rememberForever('attributes.' . config('rapidez.store'), function () {
57+
$attributes = Cache::store('rapidez:multi')->tags('attributes')->rememberForever('attributes.' . config('rapidez.store'), function () {
5858
return self::all()->toArray();
5959
});
6060

src/Models/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function getValue(
8484
}
8585

8686
if ($options['cache'] ?? true) {
87-
$configCache = Cache::driver('rapidez:multi')->get('magento.config', []);
87+
$configCache = Cache::driver('rapidez:multi')->tags('config')->get('magento.config', []);
8888
$cacheKey = implode(
8989
'.',
9090
[
@@ -124,7 +124,7 @@ public static function getValue(
124124

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

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