Skip to content

EspoCRM – Open Source CRM Application

Cache inteface

Yuriia618c3fc6caf
diff --git a/application/Espo/Core/Acl/Table/DefaultTable.php b/application/Espo/Core/Acl/Table/DefaultTable.php
index 8c3aae51ed..afa155bf76 100644
--- a/application/Espo/Core/Acl/Table/DefaultTable.php
+++ b/application/Espo/Core/Acl/Table/DefaultTable.php
@@ -115,12 +115,18 @@ class DefaultTable implements Table
$this->cacheKey = $cacheKeyProvider->get();
+ $cachedData = null;
+
if ($systemConfig->useCache() && $dataCache->has($this->cacheKey)) {
- /** @var stdClass $cachedData */
+ /** @var ?stdClass $cachedData */
$cachedData = $dataCache->get($this->cacheKey);
- $this->data = $cachedData;
- } else {
+ if ($cachedData !== null) {
+ $this->data = $cachedData;
+ }
+ }
+
+ if ($cachedData === null) {
$this->load();
if ($systemConfig->useCache()) {
diff --git a/application/Espo/Core/Container/ContainerBuilder.php b/application/Espo/Core/Container/ContainerBuilder.php
index c92be133d5..b612760811 100644
--- a/application/Espo/Core/Container/ContainerBuilder.php
+++ b/application/Espo/Core/Container/ContainerBuilder.php
@@ -38,6 +38,7 @@ use Espo\Core\Binding\BindingLoader;
use Espo\Core\Binding\EspoBindingLoader;
use Espo\Core\Loaders\ApplicationState as ApplicationStateLoader;
+use Espo\Core\Utils\Cache\FileCacheItemPool;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Config\ConfigFileManager;
use Espo\Core\Utils\Config;
@@ -61,8 +62,6 @@ class ContainerBuilder
private string $configClassName = Config::class;
/** @var class-string */
private string $fileManagerClassName = FileManager::class;
- /** @var class-string */
- private string $dataCacheClassName = DataCache::class;
/** @var class-string<Module> */
private string $moduleClassName = Module::class;
private ?BindingLoader $bindingLoader = null;
@@ -157,17 +156,6 @@ class ContainerBuilder
return $this;
}
- /**
- * @param class-string $dataCacheClassName
- * @noinspection PhpUnused
- */
- public function withDataCacheClassName(string $dataCacheClassName): self
- {
- $this->dataCacheClassName = $dataCacheClassName;
-
- return $this;
- }
-
public function build(): ContainerInterface
{
$this->services['applicationParams'] = $this->params ?? new ApplicationParams();
@@ -186,9 +174,11 @@ class ContainerBuilder
)
);
+ $cacheItemPool = new FileCacheItemPool($fileManager);
+
/** @var DataCache $dataCache */
$dataCache = $this->services['dataCache'] ?? (
- new $this->dataCacheClassName($fileManager)
+ new DataCache($cacheItemPool)
);
$useCache = $config->get('useCache') ?? false;
diff --git a/application/Espo/Core/Hook/DataProvider.php b/application/Espo/Core/Hook/DataProvider.php
index 2d655af91d..2a064956f4 100644
--- a/application/Espo/Core/Hook/DataProvider.php
+++ b/application/Espo/Core/Hook/DataProvider.php
@@ -82,12 +82,14 @@ class DataProvider
private function load(): void
{
if ($this->systemConfig->useCache() && $this->dataCache->has($this->cacheKey)) {
- /** @var array<string, array<string, mixed>> $cachedData */
+ /** @var ?array<string, array<string, mixed>> $cachedData */
$cachedData = $this->dataCache->get($this->cacheKey);
- $this->data = $cachedData;
+ if ($cachedData !== null) {
+ $this->data = $cachedData;
- return;
+ return;
+ }
}
$data = $this->readHookData($this->pathProvider->getCustom() . 'Hooks');
diff --git a/application/Espo/Core/Utils/Autoload.php b/application/Espo/Core/Utils/Autoload.php
index d1a8724826..59f5b033ce 100644
--- a/application/Espo/Core/Utils/Autoload.php
+++ b/application/Espo/Core/Utils/Autoload.php
@@ -75,9 +75,11 @@ class Autoload
/** @var ?array<string, mixed> $data */
$data = $this->dataCache->get($this->cacheKey);
- $this->data = $data;
+ if ($data !== null) {
+ $this->data = $data;
- return;
+ return;
+ }
}
$this->data = $this->loadData();
diff --git a/application/Espo/Core/Utils/Autoload/NamespaceLoader.php b/application/Espo/Core/Utils/Autoload/NamespaceLoader.php
index 3a3f2dd8a4..e5794e26f0 100644
--- a/application/Espo/Core/Utils/Autoload/NamespaceLoader.php
+++ b/application/Espo/Core/Utils/Autoload/NamespaceLoader.php
@@ -240,12 +240,10 @@ class NamespaceLoader
/** @var ?array<string, mixed> $cachedData */
$cachedData = $this->dataCache->get($this->cacheKey);
- $this->vendorNamespaces = $cachedData;
+ $this->vendorNamespaces = $cachedData ?? [];
}
}
- assert($this->vendorNamespaces !== null);
-
if (!array_key_exists($path, $this->vendorNamespaces)) {
$vendorPath = $this->findVendorPath($path);
diff --git a/application/Espo/Core/Utils/Cache/CacheItem.php b/application/Espo/Core/Utils/Cache/CacheItem.php
new file mode 100644
index 0000000000..90729fadda
--- /dev/null
+++ b/application/Espo/Core/Utils/Cache/CacheItem.php
@@ -0,0 +1,81 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Cache;
+
+use DateInterval;
+use DateTimeInterface;
+use Psr\Cache\CacheItemInterface;
+
+class CacheItem implements CacheItemInterface
+{
+ public function __construct(
+ private string $key,
+ private mixed $value,
+ private bool $isHit = false,
+ ) {}
+
+ public function getKey(): string
+ {
+ return $this->key;
+ }
+
+ public function get(): mixed
+ {
+ return $this->value;
+ }
+
+ public function isHit(): bool
+ {
+ return $this->isHit;
+ }
+
+ public function set(mixed $value): static
+ {
+ $this->value = $value;
+
+ return $this;
+ }
+
+ /**
+ * Not supported.
+ */
+ public function expiresAt(?DateTimeInterface $expiration): static
+ {
+ return $this;
+ }
+
+ /**
+ * Not supported.
+ */
+ public function expiresAfter(DateInterval|int|null $time): static
+ {
+ return $this;
+ }
+}
diff --git a/application/Espo/Core/Utils/Cache/DataCacheAccess.php b/application/Espo/Core/Utils/Cache/DataCacheAccess.php
index d91f155150..2aa2bd49b1 100644
--- a/application/Espo/Core/Utils/Cache/DataCacheAccess.php
+++ b/application/Espo/Core/Utils/Cache/DataCacheAccess.php
@@ -30,6 +30,7 @@
namespace Espo\Core\Utils\Cache;
use Closure;
+use Espo\Core\Utils\Cache\Exceptions\ReadError;
use Espo\Core\Utils\Config\SystemConfig;
use Espo\Core\Utils\DataCache;
use Espo\Core\Utils\Log;
@@ -40,6 +41,7 @@ use stdClass;
* @internal
* @since 10.1.0
* @template T of array<int|string, mixed> | stdClass = array<int|string, mixed> | stdClass
+ * @todo Test.
*/
class DataCacheAccess
{
@@ -140,24 +142,31 @@ class DataCacheAccess
{
$key = $this->key ?? throw new LogicException();
- $data = $this->dataCache->tryGet($key);
+ try {
+ $data = $this->dataCache->get($key);
+ } catch (ReadError $e) {
+ $this->log->warning("Corrupted cache data by key '{key}'.", [
+ 'exception' => $e,
+ 'key' => $key,
+ ]);
- if (is_array($data) || $data instanceof stdClass) {
- /** @var T $data */
+ $this->dataCache->clear($key);
- if ($this->validityChecker && !($this->validityChecker)($data)) {
- $this->data = null;
+ return;
+ }
- return;
- }
+ if ($data === null) {
+ return;
+ }
- $this->data = $data;
+ /** @var T $data */
+
+ if ($this->validityChecker && !($this->validityChecker)($data)) {
+ $this->data = null;
return;
}
- $this->log->warning("Corrupted cache data in '$key'.");
-
- $this->dataCache->clear($key);
+ $this->data = $data;
}
}
diff --git a/application/Espo/Core/Utils/Cache/Exceptions/InvalidArgument.php b/application/Espo/Core/Utils/Cache/Exceptions/InvalidArgument.php
new file mode 100644
index 0000000000..5be77e21ef
--- /dev/null
+++ b/application/Espo/Core/Utils/Cache/Exceptions/InvalidArgument.php
@@ -0,0 +1,39 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Cache\Exceptions;
+
+use Psr\Cache\InvalidArgumentException as InvalidArgumentExceptionInterface;
+use RuntimeException;
+
+/**
+ * @since 10.1.0
+ */
+class InvalidArgument extends RuntimeException implements InvalidArgumentExceptionInterface
+{}
diff --git a/application/Espo/Core/Utils/Cache/Exceptions/PersistenceError.php b/application/Espo/Core/Utils/Cache/Exceptions/PersistenceError.php
new file mode 100644
index 0000000000..73fb7d5481
--- /dev/null
+++ b/application/Espo/Core/Utils/Cache/Exceptions/PersistenceError.php
@@ -0,0 +1,38 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Cache\Exceptions;
+
+use RuntimeException;
+
+/**
+ * @since 10.1.0
+ */
+class PersistenceError extends RuntimeException
+{}
diff --git a/application/Espo/Core/Utils/Cache/Exceptions/ReadError.php b/application/Espo/Core/Utils/Cache/Exceptions/ReadError.php
new file mode 100644
index 0000000000..d0c3256869
--- /dev/null
+++ b/application/Espo/Core/Utils/Cache/Exceptions/ReadError.php
@@ -0,0 +1,38 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Cache\Exceptions;
+
+use RuntimeException;
+
+/**
+ * @since 10.1.0
+ */
+class ReadError extends RuntimeException
+{}
diff --git a/application/Espo/Core/Utils/Cache/FileCacheItemPool.php b/application/Espo/Core/Utils/Cache/FileCacheItemPool.php
new file mode 100644
index 0000000000..b380236898
--- /dev/null
+++ b/application/Espo/Core/Utils/Cache/FileCacheItemPool.php
@@ -0,0 +1,176 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Cache;
+
+use Espo\Core\Utils\Cache\Exceptions\InvalidArgument;
+use Espo\Core\Utils\File\Exceptions\FileError;
+use Espo\Core\Utils\File\Manager as FileManager;
+use InvalidArgumentException;
+use Psr\Cache\CacheItemInterface;
+use Psr\Cache\CacheItemPoolInterface;
+
+/**
+ * Supports only array and stdClass values.
+ *
+ * @since 10.1.0
+ */
+class FileCacheItemPool implements CacheItemPoolInterface
+{
+ private string $cacheDir = 'data/cache/application/';
+
+ public function __construct(private FileManager $fileManager)
+ {}
+
+ /**
+ * @throws InvalidArgument
+ */
+ public function getItem(string $key): CacheItem
+ {
+ $file = $this->getFile($key);
+
+ try {
+ $value = $this->fileManager->getPhpSafeContents($file);
+ } catch (FileError) {
+ return new CacheItem(
+ key: $key,
+ value: null,
+ isHit: false,
+ );
+ }
+
+ return new CacheItem(
+ key: $key,
+ value: $value,
+ isHit: true,
+ );
+ }
+
+ /**
+ * @return iterable<CacheItem>
+ */
+ public function getItems(array $keys = []): iterable
+ {
+ $values = [];
+
+ foreach ($keys as $key) {
+ $values[] = $this->getItem($key);
+ }
+
+ return $values;
+ }
+
+ /**
+ * @throws InvalidArgument
+ */
+ public function hasItem(string $key): bool
+ {
+ $file = $this->getFile($key);
+
+ return $this->fileManager->isFile($file);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function clear(): bool
+ {
+ return $this->fileManager->removeInDir($this->cacheDir);
+ }
+
+ /**
+ * @inheritDoc
+ * @throws InvalidArgument
+ */
+ public function deleteItem(string $key): bool
+ {
+ $file = $this->getFile($key);
+
+ return $this->fileManager->removeFile($file);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function deleteItems(array $keys): bool
+ {
+ $result = true;
+
+ foreach ($keys as $key) {
+ $result &= $this->deleteItem($key);
+ }
+
+ return (bool) $result;
+ }
+
+ /**
+ * Persists a cache immediately.
+ *
+ * @todo Throw an error in DataCache if false.
+ */
+ public function save(CacheItemInterface $item): bool
+ {
+ $file = $this->getFile($item->getKey());
+
+ $result = $this->fileManager->putPhpContents($file, $item->get(), true, true);
+
+ if ($result === false) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Sets a cache item to be persisted later.
+ */
+ public function saveDeferred(CacheItemInterface $item): bool
+ {
+ return $this->save($item);
+ }
+
+ public function commit(): bool
+ {
+ return true;
+ }
+
+ private function getFile(string $key): string
+ {
+ if (
+ $key === '' ||
+ preg_match('/[^a-zA-Z0-9_\/\-]/i', $key) ||
+ $key[0] === '/' ||
+ str_ends_with($key, '/')
+ ) {
+ throw new InvalidArgumentException("Bad cache key.");
+ }
+
+ return $this->cacheDir . $key . '.php';
+ }
+}
diff --git a/application/Espo/Core/Utils/DataCache.php b/application/Espo/Core/Utils/DataCache.php
index f7509c8c90..2fcdae5096 100644
--- a/application/Espo/Core/Utils/DataCache.php
+++ b/application/Espo/Core/Utils/DataCache.php
@@ -29,60 +29,67 @@
namespace Espo\Core\Utils;
-use Espo\Core\Utils\File\Exceptions\FileError;
-use Espo\Core\Utils\File\Manager as FileManager;
-
+use Espo\Core\Utils\Cache\CacheItem;
+use Espo\Core\Utils\Cache\Exceptions\InvalidArgument;
+use Espo\Core\Utils\Cache\Exceptions\PersistenceError;
+use Espo\Core\Utils\Cache\Exceptions\ReadError;
+use Espo\Core\Utils\Cache\FileCacheItemPool;
use InvalidArgumentException;
-use RuntimeException;
use stdClass;
class DataCache
{
- protected string $cacheDir = 'data/cache/application/';
-
- public function __construct(protected FileManager $fileManager)
- {}
+ public function __construct(
+ private FileCacheItemPool $fileCacheItemPool,
+ ) {}
/**
* Whether is cached.
*/
public function has(string $key): bool
{
- $cacheFile = $this->getCacheFile($key);
-
- return $this->fileManager->isFile($cacheFile);
+ return $this->fileCacheItemPool->hasItem($key);
}
/**
- * Get a stored value.
+ * Get a stored value. Returns null if not hit.
*
- * @return array<int|string, mixed>|stdClass
- * @throws FileError
+ * @return array<int|string, mixed>|stdClass|null
+ * @throws ReadError If data is corrupted.
*/
- public function get(string $key)
+ public function get(string $key): array|stdClass|null
{
- $cacheFile = $this->getCacheFile($key);
+ $item = $this->fileCacheItemPool->getItem($key);
+
+ if (!$item->isHit()) {
+ return null;
+ }
+
+ $value = $item->get();
+
+ if (!is_array($value) && !$value instanceof stdClass) {
+ throw new ReadError("Bad cache data by key '$key'.");
+ }
- return $this->fileManager->getPhpSafeContents($cacheFile);
+ /** @var array<int|string, mixed>|stdClass */
+ return $value;
}
/**
- * Try to get a stored value. Returns null if does not exist.
+ * Try to get a stored value. Does not throw ReadError.
*
* @return array<int|string, mixed>|stdClass|null
* @since 9.3.0
*/
- public function tryGet(string $key)
+ public function tryGet(string $key): array|stdClass|null
{
if (!$this->has($key)) {
return null;
}
- $cacheFile = $this->getCacheFile($key);
-
try {
- return $this->fileManager->getPhpSafeContents($cacheFile);
- } catch (FileError) {
+ return $this->get($key);
+ } catch (ReadError) {
return null;
}
}
@@ -91,6 +98,7 @@ class DataCache
* Store in cache.
*
* @param array<int|string, mixed>|stdClass $data
+ * @throws PersistenceError
*/
public function store(string $key, $data): void
{
@@ -100,23 +108,26 @@ class DataCache
throw new InvalidArgumentException("Bad cache data type.");
}
- $cacheFile = $this->getCacheFile($key);
+ $item = new CacheItem(
+ key: $key,
+ value: $data,
+ );
- $result = $this->fileManager->putPhpContents($cacheFile, $data, true, true);
+ $result = $this->fileCacheItemPool->save($item);
if ($result === false) {
- throw new RuntimeException("Could not store '$key'.");
+ throw new PersistenceError("Could not store '$key'.");
}
}
/**
* Removes in cache.
+ *
+ * @throws InvalidArgument
*/
public function clear(string $key): void
{
- $cacheFile = $this->getCacheFile($key);
-
- $this->fileManager->removeFile($cacheFile);
+ $this->fileCacheItemPool->deleteItem($key);
}
/**
@@ -131,18 +142,4 @@ class DataCache
return !$isInvalid;
}
-
- private function getCacheFile(string $key): string
- {
- if (
- $key === '' ||
- preg_match('/[^a-zA-Z0-9_\/\-]/i', $key) ||
- $key[0] === '/' ||
- str_ends_with($key, '/')
- ) {
- throw new InvalidArgumentException("Bad cache key.");
- }
-
- return $this->cacheDir . $key . '.php';
- }
}
diff --git a/application/Espo/Core/Utils/EmailFilterManager.php b/application/Espo/Core/Utils/EmailFilterManager.php
index ef8acd4247..cf3fca53eb 100644
--- a/application/Espo/Core/Utils/EmailFilterManager.php
+++ b/application/Espo/Core/Utils/EmailFilterManager.php
@@ -86,11 +86,15 @@ class EmailFilterManager
$cacheKey = $this->composeCacheKey($userId);
if ($this->useCache && $this->dataCache->has($cacheKey)) {
- $this->data[$userId] = $this->loadFromCache($cacheKey);
+ $cached = $this->loadFromCache($cacheKey);
- $this->setCacheVersionNumber($userId);
+ if ($cached !== null) {
+ $this->data[$userId] = $cached;
- return $this->data[$userId];
+ $this->setCacheVersionNumber($userId);
+
+ return $this->data[$userId];
+ }
}
$this->data[$userId] = $this->fetch($userId);
@@ -136,13 +140,17 @@ class EmailFilterManager
}
/**
- * @return EmailFilter[]
+ * @return ?EmailFilter[]
*/
- private function loadFromCache(string $cacheKey): array
+ private function loadFromCache(string $cacheKey): ?array
{
/** @var stdClass[] $dataList */
$dataList = $this->dataCache->get($cacheKey);
+ if ($dataList === null) {
+ return null;
+ }
+
/** @var EmailFilter[] $list */
$list = [];
diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php
index 5c03b62a46..06154bf07a 100644
--- a/application/Espo/Core/Utils/Language.php
+++ b/application/Espo/Core/Utils/Language.php
@@ -451,34 +451,35 @@ class Language
$cacheKey = $this->getCacheKey($language);
- if (!$this->useCache || !$this->dataCache->has($cacheKey) || $reload) {
- $readerParams = ResourceReaderParams
- ::create()
- ->withNoCustom($this->noCustom);
-
- $path = str_replace('{language}', $language, $this->resourcePath);
+ if (!$reload && $this->useCache && $this->dataCache->has($cacheKey)) {
+ /** @var ?array<string, mixed> $cachedData */
+ $cachedData = $this->dataCache->get($cacheKey);
- $data = $this->resourceReader->readAsArray($path, $readerParams);
+ if ($cachedData !== null) {
+ $this->data[$language] = $cachedData;
- if ($language !== $this->defaultLanguage && !$this->noFallback) {
- /** @var array<string, array<string, mixed>> $data */
- $data = Util::merge($this->getDefaultLanguageData($reload), $data);
+ return $cachedData;
}
+ }
- $this->data[$language] = $data;
+ $readerParams = ResourceReaderParams::create()
+ ->withNoCustom($this->noCustom);
- if ($this->useCache) {
- $this->dataCache->store($cacheKey, $data);
- }
+ $path = str_replace('{language}', $language, $this->resourcePath);
+
+ $data = $this->resourceReader->readAsArray($path, $readerParams);
+
+ if ($language !== $this->defaultLanguage && !$this->noFallback) {
+ /** @var array<string, array<string, mixed>> $data */
+ $data = Util::merge($this->getDefaultLanguageData($reload), $data);
}
- if ($this->useCache) {
- /** @var array<string, mixed> $cachedData */
- $cachedData = $this->dataCache->get($cacheKey);
+ $this->data[$language] = $data;
- $this->data[$language] = $cachedData;
+ if ($this->useCache) {
+ $this->dataCache->store($cacheKey, $data);
}
- return $this->data[$language] ?? [];
+ return $data;
}
}
diff --git a/application/Espo/Core/Utils/Metadata/OrmMetadataData.php b/application/Espo/Core/Utils/Metadata/OrmMetadataData.php
index 77a1942670..64b2a7ce5e 100644
--- a/application/Espo/Core/Utils/Metadata/OrmMetadataData.php
+++ b/application/Espo/Core/Utils/Metadata/OrmMetadataData.php
@@ -88,12 +88,14 @@ class OrmMetadataData
}
if ($this->useCache && $this->dataCache->has($this->cacheKey) && !$reload) {
- /** @var array<string, array<string, mixed>> $data */
+ /** @var ?array<string, array<string, mixed>> $data */
$data = $this->dataCache->get($this->cacheKey);
- $this->data = $data;
+ if ($data !== null) {
+ $this->data = $data;
- return $this->data;
+ return $this->data;
+ }
}
$this->data = $this->getConverter()->process();
diff --git a/application/Espo/Core/Utils/Module.php b/application/Espo/Core/Utils/Module.php
index 7085fd5dc1..8f36200fe4 100644
--- a/application/Espo/Core/Utils/Module.php
+++ b/application/Espo/Core/Utils/Module.php
@@ -87,12 +87,14 @@ class Module
$this->dataCache &&
$this->dataCache->has($this->cacheKey)
) {
- /** @var array<string, array<string, mixed>> $data */
+ /** @var ?array<string, array<string, mixed>> $data */
$data = $this->dataCache->get($this->cacheKey);
- $this->data = $data;
+ if ($data !== null) {
+ $this->data = $data;
- return;
+ return;
+ }
}
$this->data = $this->loadData();
diff --git a/application/Espo/Core/Utils/Route.php b/application/Espo/Core/Utils/Route.php
index aebfe2d6cf..9cf4c0bb34 100644
--- a/application/Espo/Core/Utils/Route.php
+++ b/application/Espo/Core/Utils/Route.php
@@ -97,9 +97,11 @@ class Route
/** @var ?(RouteArrayShape[]) $data */
$data = $this->dataCache->get($this->cacheKey);
- $this->data = $data;
+ if ($data !== null) {
+ $this->data = $data;
- return;
+ return;
+ }
}
$this->data = $this->unify();
diff --git a/application/Espo/Tools/App/Language/AclDependencyProvider.php b/application/Espo/Tools/App/Language/AclDependencyProvider.php
index b94f2b9c00..96fd6d2e27 100644
--- a/application/Espo/Tools/App/Language/AclDependencyProvider.php
+++ b/application/Espo/Tools/App/Language/AclDependencyProvider.php
@@ -78,10 +78,12 @@ class AclDependencyProvider
private function loadData(): array
{
if ($this->useCache && $this->dataCache->has(self::CACHE_KEY)) {
- /** @var array<string, mixed>[] $raw */
+ /** @var ?array<string, mixed>[] $raw */
$raw = $this->dataCache->get(self::CACHE_KEY);
- return $this->buildFromRaw($raw);
+ if ($raw !== null) {
+ return $this->buildFromRaw($raw);
+ }
}
return $this->buildData();
diff --git a/application/Espo/Tools/App/Metadata/AclDependencyProvider.php b/application/Espo/Tools/App/Metadata/AclDependencyProvider.php
index 4ce03807e8..e9ba71cc3c 100644
--- a/application/Espo/Tools/App/Metadata/AclDependencyProvider.php
+++ b/application/Espo/Tools/App/Metadata/AclDependencyProvider.php
@@ -78,10 +78,12 @@ class AclDependencyProvider
private function loadData(): array
{
if ($this->useCache && $this->dataCache->has(self::CACHE_KEY)) {
- /** @var array<string, mixed>[] $raw */
+ /** @var ?array<string, mixed>[] $raw */
$raw = $this->dataCache->get(self::CACHE_KEY);
- return $this->buildFromRaw($raw);
+ if ($raw !== null) {
+ return $this->buildFromRaw($raw);
+ }
}
return $this->buildData();
diff --git a/application/Espo/Tools/LeadCapture/FormService.php b/application/Espo/Tools/LeadCapture/FormService.php
index dc8c8b4ea0..5258b2c620 100644
--- a/application/Espo/Tools/LeadCapture/FormService.php
+++ b/application/Espo/Tools/LeadCapture/FormService.php
@@ -91,7 +91,11 @@ class FormService
$cacheKey = $this->getCacheKey($leadCapture);
if ($this->systemConfig->useCache() && $this->dataCache->has($cacheKey)) {
- return $this->getFromCache($cacheKey);
+ $cached = $this->getFromCache($cacheKey);
+
+ if ($cached !== null) {
+ return $cached;
+ }
}
$data = $this->prepareData($leadCapture);
@@ -389,11 +393,11 @@ class FormService
}
/**
- * @return array<string, mixed>
+ * @return ?array<string, mixed>
*/
- private function getFromCache(string $cacheKey): array
+ private function getFromCache(string $cacheKey): ?array
{
- /** @var array<string, mixed> */
+ /** @var ?array<string, mixed> */
return $this->dataCache->get($cacheKey);
}
diff --git a/application/Espo/Tools/OpenApi/Provider.php b/application/Espo/Tools/OpenApi/Provider.php
index 09f2b2a556..0752d37e52 100644
--- a/application/Espo/Tools/OpenApi/Provider.php
+++ b/application/Espo/Tools/OpenApi/Provider.php
@@ -90,11 +90,13 @@ class Provider
if ($this->dataCache->has($cacheKey)) {
$data = $this->dataCache->get($cacheKey);
- if (!$data instanceof stdClass) {
+ if ($data !== null && !$data instanceof stdClass) {
throw new RuntimeException("Corrupted OpenAPI spec cache file.");
}
- return $data;
+ if ($data !== null) {
+ return $data;
+ }
}
$data = $this->buildData($params);
diff --git a/application/Espo/Tools/Pipeline/PipelineDataProvider.php b/application/Espo/Tools/Pipeline/PipelineDataProvider.php
index 22c86eaaf2..5f6b0c347d 100644
--- a/application/Espo/Tools/Pipeline/PipelineDataProvider.php
+++ b/application/Espo/Tools/Pipeline/PipelineDataProvider.php
@@ -81,7 +81,7 @@ class PipelineDataProvider
if ($this->systemConfig->useCache()) {
$data = $this->getFromCache();
- if (!$data) {
+ if ($data === null) {
$store = true;
}
}
@@ -300,6 +300,10 @@ class PipelineDataProvider
{
$data = $this->dataCache->get(self::CACHE_KEY);
+ if ($data === null) {
+ return null;
+ }
+
if (!$data instanceof stdClass) {
$this->log->warning("Bad pipeline cache.");
diff --git a/composer.json b/composer.json
index 69fddaf364..4b059737b0 100644
--- a/composer.json
+++ b/composer.json
@@ -58,7 +58,8 @@
"guzzlehttp/guzzle": "^7.10",
"devtheorem/php-handlebars": "^1.0",
"php-amqplib/php-amqplib": "^3.7",
- "predis/predis": "^3.5"
+ "predis/predis": "^3.5",
+ "psr/cache": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^11.5",
diff --git a/composer.lock b/composer.lock
index a7d45b5359..0ab3cdb7e9 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "1ae60dc55489013607247b563b8935f3",
+ "content-hash": "038edf6480a482ceac6acb6a643eee35",
"packages": [
{
"name": "async-aws/core",
diff --git a/tests/unit/Espo/Core/Utils/DataCacheTest.php b/tests/unit/Espo/Core/Utils/DataCacheTest.php
index 1c9cef4a70..b8b4cfb86c 100644
--- a/tests/unit/Espo/Core/Utils/DataCacheTest.php
+++ b/tests/unit/Espo/Core/Utils/DataCacheTest.php
@@ -29,23 +29,25 @@
namespace tests\unit\Espo\Core\Utils;
+use Espo\Core\Utils\Cache\FileCacheItemPool;
use Espo\Core\Utils\DataCache;
use Espo\Core\Utils\File\Manager as FileManager;
-
use PHPUnit\Framework\TestCase;
use RuntimeException;
use InvalidArgumentException;
class DataCacheTest extends TestCase
{
- private $fileManager;
- private $dataCache;
+ private ?FileManager $fileManager = null;
+ private ?DataCache $dataCache = null;
protected function setUp() : void
{
$this->fileManager = $this->createMock(FileManager::class);
- $this->dataCache = new DataCache($this->fileManager);
+ $pool = new FileCacheItemPool($this->fileManager);
+
+ $this->dataCache = new DataCache($pool);
}
public function testHasTrue()