snapshot of https://github.com/espocrm/espocrm.git (master, 2026-09-18; blobs > 131072B omitted)
Some checks failed
Test / Test on PHP 8.3 (push) Failing after 5s
Test / Test on PHP 8.4 (push) Failing after 4s
Test / Test on PHP 8.5 (push) Failing after 3s
TypeScript static analysis / TypeScript check (push) Failing after 14s
Test Integration on MySQL / Test on PHP 8.3 (push) Failing after 2s
Test Integration on MySQL / Test on PHP 8.5 (push) Failing after 0s
Test Integration on PostgreSQL / Test on PHP 8.4 fix (push) Failing after 1s
Test Integration on PostgreSQL / Test on PHP 8.4 master (push) Failing after 1s
Some checks failed
Test / Test on PHP 8.3 (push) Failing after 5s
Test / Test on PHP 8.4 (push) Failing after 4s
Test / Test on PHP 8.5 (push) Failing after 3s
TypeScript static analysis / TypeScript check (push) Failing after 14s
Test Integration on MySQL / Test on PHP 8.3 (push) Failing after 2s
Test Integration on MySQL / Test on PHP 8.5 (push) Failing after 0s
Test Integration on PostgreSQL / Test on PHP 8.4 fix (push) Failing after 1s
Test Integration on PostgreSQL / Test on PHP 8.4 master (push) Failing after 1s
This commit is contained in:
81
application/Espo/Core/Utils/Cache/CacheItem.php
Normal file
81
application/Espo/Core/Utils/Cache/CacheItem.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
172
application/Espo/Core/Utils/Cache/DataCacheAccess.php
Normal file
172
application/Espo/Core/Utils/Cache/DataCacheAccess.php
Normal file
@@ -0,0 +1,172 @@
|
||||
<?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 Closure;
|
||||
use Espo\Core\Utils\Cache\Exceptions\ReadError;
|
||||
use Espo\Core\Utils\Config\SystemConfig;
|
||||
use Espo\Core\Utils\DataCache;
|
||||
use Espo\Core\Utils\Log;
|
||||
use LogicException;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @since 10.1.0
|
||||
* @template T of array<int|string, mixed> | stdClass = array<int|string, mixed> | stdClass
|
||||
* @todo Test.
|
||||
*/
|
||||
class DataCacheAccess
|
||||
{
|
||||
/** @var T|null */
|
||||
private mixed $data = null;
|
||||
|
||||
private ?string $key = null;
|
||||
|
||||
/** @var (Closure(): T)|null */
|
||||
private ?Closure $loader = null;
|
||||
|
||||
/** @var (Closure(T): bool)|null */
|
||||
private ?Closure $validityChecker = null;
|
||||
|
||||
public function __construct(
|
||||
private DataCache $dataCache,
|
||||
private SystemConfig $systemConfig,
|
||||
private Log $log,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param Closure(): T $loader
|
||||
* @param (Closure(T): bool)|null $validityChecker
|
||||
*/
|
||||
public function init(string $key, Closure $loader, ?Closure $validityChecker = null): void
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->loader = $loader;
|
||||
$this->validityChecker = $validityChecker;
|
||||
|
||||
$this->data = null;
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->data = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param T $data
|
||||
*/
|
||||
public function set(mixed $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function store(): void
|
||||
{
|
||||
if (!$this->key) {
|
||||
throw new LogicException("Not initialized.");
|
||||
}
|
||||
|
||||
if ($this->data === null) {
|
||||
throw new LogicException("Data not set.");
|
||||
}
|
||||
|
||||
if ($this->systemConfig->useCache()) {
|
||||
$this->dataCache->store($this->key, $this->data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return T
|
||||
*/
|
||||
public function get(): mixed
|
||||
{
|
||||
if ($this->data && $this->validityChecker && !($this->validityChecker)($this->data)) {
|
||||
$this->data = null;
|
||||
}
|
||||
|
||||
if ($this->data !== null) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
$key = $this->key;
|
||||
$loader = $this->loader;
|
||||
|
||||
if (!$key || !$loader) {
|
||||
throw new LogicException("Not initialized.");
|
||||
}
|
||||
|
||||
if ($this->systemConfig->useCache() && $this->dataCache->has($key)) {
|
||||
$this->loadFromCache();
|
||||
}
|
||||
|
||||
if ($this->data === null) {
|
||||
$this->data = $loader();
|
||||
|
||||
if ($this->systemConfig->useCache()) {
|
||||
$this->dataCache->store($key, $this->data);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
private function loadFromCache(): void
|
||||
{
|
||||
$key = $this->key ?? throw new LogicException();
|
||||
|
||||
try {
|
||||
$data = $this->dataCache->get($key);
|
||||
} catch (ReadError $e) {
|
||||
$this->log->warning("Corrupted cache data by key '{key}'.", [
|
||||
'exception' => $e,
|
||||
'key' => $key,
|
||||
]);
|
||||
|
||||
$this->dataCache->clear($key);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($data === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var T $data */
|
||||
|
||||
if ($this->validityChecker && !($this->validityChecker)($data)) {
|
||||
$this->data = null;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
||||
52
application/Espo/Core/Utils/Cache/DataCacheProvider.php
Normal file
52
application/Espo/Core/Utils/Cache/DataCacheProvider.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?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\Container;
|
||||
use Espo\Core\Utils\DataCache;
|
||||
|
||||
/**
|
||||
* May be needed when data cache is corrupted not allowing to load the DataCache service
|
||||
* when clearing cache.
|
||||
*
|
||||
* @internal
|
||||
* @since 10.1.0
|
||||
*/
|
||||
class DataCacheProvider
|
||||
{
|
||||
public function __construct(
|
||||
private Container $container,
|
||||
) {}
|
||||
|
||||
public function get(): DataCache
|
||||
{
|
||||
return $this->container->getByClass(DataCache::class);
|
||||
}
|
||||
}
|
||||
56
application/Espo/Core/Utils/Cache/DataCacheServiceLoader.php
Normal file
56
application/Espo/Core/Utils/Cache/DataCacheServiceLoader.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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\Container;
|
||||
use Espo\Core\Container\Loader;
|
||||
use Espo\Core\Utils\DataCache;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class DataCacheServiceLoader implements Loader
|
||||
{
|
||||
public function __construct(
|
||||
private Container $container,
|
||||
) {}
|
||||
|
||||
public function load(): DataCache
|
||||
{
|
||||
$service = $this->container->get(DataCacheServiceName::SYSTEM);
|
||||
|
||||
if (!$service instanceof DataCache) {
|
||||
throw new RuntimeException("Unexpected 'dataCache' service instance.");
|
||||
}
|
||||
|
||||
return $service;
|
||||
}
|
||||
}
|
||||
39
application/Espo/Core/Utils/Cache/DataCacheServiceName.php
Normal file
39
application/Espo/Core/Utils/Cache/DataCacheServiceName.php
Normal file
@@ -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;
|
||||
|
||||
/**
|
||||
* @since 10.1.0
|
||||
*/
|
||||
class DataCacheServiceName
|
||||
{
|
||||
public const string SYSTEM = 'systemDataCache';
|
||||
public const string APPLICATION = 'dataCache';
|
||||
}
|
||||
@@ -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
|
||||
{}
|
||||
@@ -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
|
||||
{}
|
||||
38
application/Espo/Core/Utils/Cache/Exceptions/ReadError.php
Normal file
38
application/Espo/Core/Utils/Cache/Exceptions/ReadError.php
Normal file
@@ -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
|
||||
{}
|
||||
176
application/Espo/Core/Utils/Cache/FileCacheItemPool.php
Normal file
176
application/Espo/Core/Utils/Cache/FileCacheItemPool.php
Normal file
@@ -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';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user