. * * 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; use Espo\Core\Utils\File\Manager as FileManager; use InvalidArgumentException; use RuntimeException; use stdClass; class DataCache { protected string $cacheDir = 'data/cache/application/'; public function __construct(protected FileManager $fileManager) {} /** * Whether is cached. */ public function has(string $key): bool { $cacheFile = $this->getCacheFile($key); return $this->fileManager->isFile($cacheFile); } /** * Get a stored value. * * @return array|stdClass */ public function get(string $key) { $cacheFile = $this->getCacheFile($key); return $this->fileManager->getPhpSafeContents($cacheFile); } /** * Store in cache. * * @param array|stdClass $data */ public function store(string $key, $data): void { /** @phpstan-var mixed $data */ if (!$this->checkDataIsValid($data)) { throw new InvalidArgumentException("Bad cache data type."); } $cacheFile = $this->getCacheFile($key); $result = $this->fileManager->putPhpContents($cacheFile, $data, true, true); if ($result === false) { throw new RuntimeException("Could not store '$key'."); } } /** * Removes in cache. */ public function clear(string $key): void { $cacheFile = $this->getCacheFile($key); $this->fileManager->removeFile($cacheFile); } /** * @param mixed $data * @return bool */ private function checkDataIsValid($data) { $isInvalid = !is_array($data) && !$data instanceof stdClass; 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'; } }