Skip to content

Commit e1906af

Browse files
committed
update cache configs
1 parent 3bd99b8 commit e1906af

3 files changed

Lines changed: 59 additions & 35 deletions

File tree

config/inspirecms.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,24 @@
7979

8080
'cache' => [
8181
'languages' => [
82+
'store' => null, // null: Fallback to default store
8283
'key' => 'inspirecms.languages',
8384
'ttl' => 60 * 60 * 24,
8485
],
8586
'navigation' => [
87+
'store' => null, // null: Fallback to default store
8688
'key' => 'inspirecms.navigation',
8789
'ttl' => 60 * 60 * 24,
8890
],
8991
'content_routes' => [
92+
'store' => null, // null: Fallback to default store
9093
'key' => 'inspirecms.content_routes',
9194
'ttl' => 120 * 60 * 24,
9295
],
9396
'key_value' => [
97+
'store' => null, // null: Fallback to default store
9498
'ttl' => 60 * 60 * 24,
99+
'prefix' => 'inspire_key_value.',
95100
],
96101
],
97102

src/Base/KeyValueCache.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010

1111
class KeyValueCache
1212
{
13-
/**
14-
* Cache prefix for key-value pairs
15-
*/
16-
protected const CACHE_PREFIX = 'inspire_key_value_';
17-
1813
/**
1914
* @var CacheManager
2015
*/
@@ -23,13 +18,19 @@ class KeyValueCache
2318
/** @var \DateInterval|int */
2419
protected $cacheExpirationTime;
2520

21+
protected $cacheStore = null;
22+
23+
protected $keyPrefix = null;
24+
2625
/**
2726
* @param CacheManager $cacheManager
2827
*/
2928
public function __construct($cacheManager, \DateInterval | int | null $ttl = null)
3029
{
3130
$this->cacheManager = $cacheManager;
3231
$this->cacheExpirationTime = $ttl ?? InspireCmsConfig::get('cache.key_value.ttl') ?? \DateInterval::createFromDateString('24 hours');
32+
$this->cacheStore = InspireCmsConfig::get('cache.key_value.store') ?? null;
33+
$this->keyPrefix = InspireCmsConfig::get('cache.key_value.prefix') ?? null;
3334
}
3435

3536
/**
@@ -64,15 +65,17 @@ public function get(string $key, $default = null)
6465
return;
6566
}
6667

67-
return $this->cacheManager->remember(
68-
$this->getCacheKey($key),
69-
$this->cacheExpirationTime,
70-
function () use ($key, $default, $model) {
71-
$keyValue = $model::findKeyValue($key);
72-
73-
return $keyValue ? $keyValue->value : $default;
74-
}
75-
);
68+
return $this->cacheManager
69+
->store($this->cacheStore)
70+
->remember(
71+
$this->getCacheKey($key),
72+
$this->cacheExpirationTime,
73+
function () use ($key, $default, $model) {
74+
$keyValue = $model::findKeyValue($key);
75+
76+
return $keyValue ? $keyValue->value : $default;
77+
}
78+
);
7679
}
7780

7881
/**
@@ -82,15 +85,19 @@ function () use ($key, $default, $model) {
8285
*/
8386
public function set(string $key, $value): void
8487
{
85-
$this->cacheManager->put($this->getCacheKey($key), $value);
88+
$this->cacheManager
89+
->store($this->cacheStore)
90+
->put($this->getCacheKey($key), $value);
8691
}
8792

8893
/**
8994
* Delete a value from the cache
9095
*/
9196
public function forget(string $key): void
9297
{
93-
$this->cacheManager->forget($this->getCacheKey($key));
98+
$this->cacheManager
99+
->store($this->cacheStore)
100+
->forget($this->getCacheKey($key));
94101
}
95102

96103
/**
@@ -116,7 +123,7 @@ public function clear(): void
116123
*/
117124
protected function getCacheKey(string $key): string
118125
{
119-
return self::CACHE_PREFIX . $key;
126+
return $this->keyPrefix . $key;
120127
}
121128

122129
/**

src/InspireCms.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ public function addSection(ClusterSection $section): void
173173
public function getAllAvailableLanguages(): array
174174
{
175175
if (! $this->cachedLanguages) {
176-
$this->cachedLanguages = $this->cacheManager->remember(
177-
InspireCmsConfig::get('cache.languages.key'),
178-
InspireCmsConfig::get('cache.languages.ttl'),
179-
fn () => $this->getSerializedLanguagesForCache()
180-
);
176+
$this->cachedLanguages = $this->cacheManager
177+
->store(InspireCmsConfig::get('cache.languages.store'))
178+
->remember(
179+
InspireCmsConfig::get('cache.languages.key'),
180+
InspireCmsConfig::get('cache.languages.ttl'),
181+
fn () => $this->getSerializedLanguagesForCache()
182+
);
181183
}
182184

183185
return collect($this->cachedLanguages['languages'] ?? [])
@@ -193,7 +195,9 @@ public function getFallbackLanguage(): ?LanguageDto
193195

194196
public function forgetCachedLanguages(): void
195197
{
196-
$this->cacheManager->forget(InspireCmsConfig::get('cache.languages.key'));
198+
$this->cacheManager
199+
->store(InspireCmsConfig::get('cache.languages.store'))
200+
->forget(InspireCmsConfig::get('cache.languages.key'));
197201
}
198202

199203
/**
@@ -202,11 +206,13 @@ public function forgetCachedLanguages(): void
202206
public function getNavigation(string $category, ?string $locale = null): array
203207
{
204208
if (! $this->cachedNavigation) {
205-
$this->cachedNavigation = $this->cacheManager->remember(
206-
InspireCmsConfig::get('cache.navigation.key'),
207-
InspireCmsConfig::get('cache.navigation.ttl'),
208-
fn () => $this->getSerializedNavigationForCache()
209-
);
209+
$this->cachedNavigation = $this->cacheManager
210+
->store(InspireCmsConfig::get('cache.navigation.store'))
211+
->remember(
212+
InspireCmsConfig::get('cache.navigation.key'),
213+
InspireCmsConfig::get('cache.navigation.ttl'),
214+
fn () => $this->getSerializedNavigationForCache()
215+
);
210216
}
211217

212218
return collect($this->cachedNavigation['navigation'] ?? [])
@@ -235,7 +241,9 @@ public function getNavigation(string $category, ?string $locale = null): array
235241

236242
public function forgetCachedNavigation(): void
237243
{
238-
$this->cacheManager->forget(InspireCmsConfig::get('cache.navigation.key'));
244+
$this->cacheManager
245+
->store(InspireCmsConfig::get('cache.content_routes.store'))
246+
->forget(InspireCmsConfig::get('cache.navigation.key'));
239247
}
240248

241249
/**
@@ -244,11 +252,13 @@ public function forgetCachedNavigation(): void
244252
public function getContentRoutes()
245253
{
246254
if (! $this->cachedContentRoutes) {
247-
$this->cachedContentRoutes = $this->cacheManager->remember(
248-
InspireCmsConfig::get('cache.content_routes.key'),
249-
InspireCmsConfig::get('cache.content_routes.ttl'),
250-
fn () => $this->getSerializedContentRoutesForCache()
251-
);
255+
$this->cachedContentRoutes = $this->cacheManager
256+
->store(InspireCmsConfig::get('cache.content_routes.store'))
257+
->remember(
258+
InspireCmsConfig::get('cache.content_routes.key'),
259+
InspireCmsConfig::get('cache.content_routes.ttl'),
260+
fn () => $this->getSerializedContentRoutesForCache()
261+
);
252262
}
253263

254264
return collect($this->cachedContentRoutes['routes'] ?? [])
@@ -258,7 +268,9 @@ public function getContentRoutes()
258268

259269
public function forgetCachedContentRoutes(): void
260270
{
261-
$this->cacheManager->forget(InspireCmsConfig::get('cache.content_routes.key'));
271+
$this->cacheManager
272+
->store(InspireCmsConfig::get('cache.content_routes.store'))
273+
->forget(InspireCmsConfig::get('cache.content_routes.key'));
262274
}
263275

264276
// region Helpers

0 commit comments

Comments
 (0)