Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

CachingHandler.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\Language;
8
9 use eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache;
10 use Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGeneratorInterface;
11 use eZ\Publish\SPI\Persistence\Content\Language;
12 use eZ\Publish\SPI\Persistence\Content\Language\Handler as BaseLanguageHandler;
13 use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct;
14
18 class CachingHandler implements BaseLanguageHandler
19 {
20 private const LANGUAGE_IDENTIFIER = 'language';
21 private const LANGUAGE_CODE_IDENTIFIER = 'language_code';
22 private const LANGUAGE_LIST_IDENTIFIER = 'language_list';
23
29 protected $innerHandler;
30
36 protected $cache;
37
39 protected $cacheIdentifierGenerator;
40
48 public function __construct(
49 BaseLanguageHandler $innerHandler,
50 InMemoryCache $cache,
51 CacheIdentifierGeneratorInterface $cacheIdentifierGenerator
52 ) {
53 $this->innerHandler = $innerHandler;
54 $this->cache = $cache;
55 $this->cacheIdentifierGenerator = $cacheIdentifierGenerator;
56 }
57
65 public function create(CreateStruct $struct)
66 {
67 $language = $this->innerHandler->create($struct);
68 $this->storeCache([$language]);
69
70 return $language;
71 }
72
78 public function update(Language $language)
79 {
80 $this->innerHandler->update($language);
81 $this->storeCache([$language]);
82 }
83
93 public function load($id)
94 {
95 $language = $this->cache->get(
96 $this->cacheIdentifierGenerator->generateKey(self::LANGUAGE_IDENTIFIER, [$id], true)
97 );
98
99 if ($language === null) {
100 $language = $this->innerHandler->load($id);
101 $this->storeCache([$language]);
102 }
103
104 return $language;
105 }
106
110 public function loadList(array $ids): iterable
111 {
112 $missing = [];
113 $languages = [];
114 foreach ($ids as $id) {
115 if ($language = $this->cache->get($this->cacheIdentifierGenerator->generateKey(self::LANGUAGE_IDENTIFIER, [$id], true))) {
116 $languages[$id] = $language;
117 } else {
118 $missing[] = $id;
119 }
120 }
121
122 if (!empty($missing)) {
123 $loaded = $this->innerHandler->loadList($missing);
124 $this->storeCache($loaded);
126 $languages += $loaded;
127 }
128
129 return $languages;
130 }
131
141 public function loadByLanguageCode($languageCode)
142 {
143 $language = $this->cache->get(
144 $this->cacheIdentifierGenerator->generateKey(self::LANGUAGE_CODE_IDENTIFIER, [$languageCode], true)
145 );
146
147 if ($language === null) {
148 $language = $this->innerHandler->loadByLanguageCode($languageCode);
149 $this->storeCache([$language]);
150 }
151
152 return $language;
153 }
154
158 public function loadListByLanguageCodes(array $languageCodes): iterable
159 {
160 $missing = [];
161 $languages = [];
162 foreach ($languageCodes as $languageCode) {
163 if ($language = $this->cache->get($this->cacheIdentifierGenerator->generateKey(self::LANGUAGE_CODE_IDENTIFIER, [$languageCode], true))) {
164 $languages[$languageCode] = $language;
165 } else {
166 $missing[] = $languageCode;
167 }
168 }
169
170 if (!empty($missing)) {
171 $loaded = $this->innerHandler->loadListByLanguageCodes($missing);
172 $this->storeCache($loaded);
174 $languages += $loaded;
175 }
176
177 return $languages;
178 }
179
185 public function loadAll()
186 {
187 $prefixedLanguageListTag = $this->cacheIdentifierGenerator->generateKey(self::LANGUAGE_LIST_IDENTIFIER, [], true);
188 $languages = $this->cache->get($prefixedLanguageListTag);
189
190 if ($languages === null) {
191 $languages = $this->innerHandler->loadAll();
192 $this->storeCache($languages, $prefixedLanguageListTag);
193 }
194
195 return $languages;
196 }
197
203 public function delete($id)
204 {
205 $this->innerHandler->delete($id);
206 // Delete by primary key will remove the object, so we don't need to clear `ez-language-code-` here.
207 $this->cache->deleteMulti([
208 $this->cacheIdentifierGenerator->generateKey(self::LANGUAGE_IDENTIFIER, [$id], true),
209 $this->cacheIdentifierGenerator->generateKey(self::LANGUAGE_LIST_IDENTIFIER, [], true),
210 ]);
211 }
212
216 public function clearCache(): void
217 {
218 $this->cache->clear();
219 }
220
227 protected function storeCache(array $languages, string $listIndex = null): void
228 {
229 $generator = $this->cacheIdentifierGenerator;
230
231 $this->cache->setMulti(
232 $languages,
233 static function (Language $language) use ($generator) {
234 return [
235 $generator->generateKey(self::LANGUAGE_IDENTIFIER, [$language->id], true),
236 $generator->generateKey(self::LANGUAGE_CODE_IDENTIFIER, [$language->languageCode], true),
237 ];
238 },
239 $listIndex
240 );
241 }
242 }
eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache
Simple internal In-Memory Cache Pool.
Definition: InMemoryCache.php:28
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler
Language Handler.
Definition: CachingHandler.php:19
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\__construct
__construct(BaseLanguageHandler $innerHandler, InMemoryCache $cache, CacheIdentifierGeneratorInterface $cacheIdentifierGenerator)
Creates a caching handler around $innerHandler.
Definition: CachingHandler.php:48
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\$cache
$cache
Definition: CachingHandler.php:36
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\create
create(CreateStruct $struct)
Create a new language.
Definition: CachingHandler.php:65
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\storeCache
storeCache(array $languages, string $listIndex=null)
Helper to store languages in internal in-memory cache with all needed keys.
Definition: CachingHandler.php:227
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\clearCache
clearCache()
Clear internal in-memory cache.
Definition: CachingHandler.php:216
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\loadByLanguageCode
loadByLanguageCode($languageCode)
Get language by Language Code (eg: eng-GB).
Definition: CachingHandler.php:141
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\$cacheIdentifierGenerator
$cacheIdentifierGenerator
Definition: CachingHandler.php:39
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\loadList
loadList(array $ids)
{Get list of languages by id.Missing items (NotFound) will be missing from the returned iterable and ...
Definition: CachingHandler.php:110
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\loadListByLanguageCodes
loadListByLanguageCodes(array $languageCodes)
{Get list of languages by Language Code (eg: eng-GB).Missing items (NotFound) will be missing from th...
Definition: CachingHandler.php:158
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\load
load($id)
Get language by id.
Definition: CachingHandler.php:93
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\LANGUAGE_LIST_IDENTIFIER
const LANGUAGE_LIST_IDENTIFIER
Definition: CachingHandler.php:22
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\LANGUAGE_CODE_IDENTIFIER
const LANGUAGE_CODE_IDENTIFIER
Definition: CachingHandler.php:21
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\loadAll
loadAll()
Get all languages.
Definition: CachingHandler.php:185
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\LANGUAGE_IDENTIFIER
const LANGUAGE_IDENTIFIER
Definition: CachingHandler.php:20
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\update
update(Language $language)
Update language.
Definition: CachingHandler.php:78
eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler\$innerHandler
$innerHandler
Definition: CachingHandler.php:29
eZ\Publish\SPI\Persistence\Content\Language\CreateStruct
Struct containing accessible properties when creating Language entities.
Definition: SPI/Persistence/Content/Language/CreateStruct.php:15
eZ\Publish\SPI\Persistence\Content\Language
Struct containing accessible properties on Language entities.
Definition: SPI/Persistence/Content/Language.php:15
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGeneratorInterface
Definition: CacheIdentifierGeneratorInterface.php:15
eZ\Publish\SPI\Persistence\Content\Language\Handler
Language Handler interface.
Definition: SPI/Persistence/Content/Language/Handler.php:15
eZ\Publish\Core\Persistence\Legacy\Content\Language
Definition: CachingHandler.php:7
eZ\Publish\SPI\Persistence\Content\Language
Definition: SPI/Persistence/Content/Language/CreateStruct.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.