Skip to content

EspoCRM – Open Source CRM Application

Redis transport dev

Yuriifa36aa933e87
diff --git a/application/Espo/Classes/RecordHooks/Team/ClearCacheAfterUnlink.php b/application/Espo/Classes/RecordHooks/Team/ClearCacheAfterUnlink.php
index 79155fdf9c..8133609b2b 100644
--- a/application/Espo/Classes/RecordHooks/Team/ClearCacheAfterUnlink.php
+++ b/application/Espo/Classes/RecordHooks/Team/ClearCacheAfterUnlink.php
@@ -30,7 +30,6 @@
namespace Espo\Classes\RecordHooks\Team;
use Espo\Core\Acl\Cache\Clearer;
-use Espo\Core\DataManager;
use Espo\Core\Record\Hook\UnlinkHook;
use Espo\Entities\Team;
use Espo\Entities\User;
@@ -43,7 +42,6 @@ class ClearCacheAfterUnlink implements UnlinkHook
{
public function __construct(
private Clearer $clearer,
- private DataManager $dataManager
) {}
public function process(Entity $entity, string $link, Entity $foreignEntity): void
@@ -53,6 +51,5 @@ class ClearCacheAfterUnlink implements UnlinkHook
}
$this->clearer->clearForUser($foreignEntity);
- $this->dataManager->updateCacheTimestamp();
}
}
diff --git a/application/Espo/Classes/RecordHooks/User/AfterUpdate.php b/application/Espo/Classes/RecordHooks/User/AfterUpdate.php
index 3026bda6e1..64c33481eb 100644
--- a/application/Espo/Classes/RecordHooks/User/AfterUpdate.php
+++ b/application/Espo/Classes/RecordHooks/User/AfterUpdate.php
@@ -30,7 +30,6 @@
namespace Espo\Classes\RecordHooks\User;
use Espo\Core\Acl\Cache\Clearer;
-use Espo\Core\DataManager;
use Espo\Core\Record\Hook\SaveHook;
use Espo\Modules\Crm\Entities\Contact;
use Espo\ORM\Entity;
@@ -46,7 +45,6 @@ class AfterUpdate implements SaveHook
public function __construct(
private EntityManager $entityManager,
private Clearer $clearer,
- private DataManager $dataManager
) {}
public function process(Entity $entity): void
@@ -61,21 +59,12 @@ class AfterUpdate implements SaveHook
$entity->isAttributeChanged('rolesIds') ||
$entity->isAttributeChanged('teamsIds') ||
$entity->isAttributeChanged('type') ||
- $entity->isAttributeChanged('portalRolesIds') ||
- $entity->isAttributeChanged('portalsIds')
- ) {
- $this->clearer->clearForUser($entity);
- $this->dataManager->updateCacheTimestamp();
- }
-
- if (
$entity->isAttributeChanged('portalRolesIds') ||
$entity->isAttributeChanged('portalsIds') ||
$entity->isAttributeChanged('contactId') ||
$entity->isAttributeChanged('accountsIds')
) {
- $this->clearer->clearForAllPortalUsers();
- $this->dataManager->updateCacheTimestamp();
+ $this->clearer->clearForUser($entity);
}
}
diff --git a/application/Espo/Core/Utils/Event/EventTransportLoader.php b/application/Espo/Core/Utils/Event/EventTransportLoader.php
index 61286d6577..0851b9ae13 100644
--- a/application/Espo/Core/Utils/Event/EventTransportLoader.php
+++ b/application/Espo/Core/Utils/Event/EventTransportLoader.php
@@ -31,6 +31,8 @@ namespace Espo\Core\Utils\Event;
use Espo\Core\Container\Loader;
use Espo\Core\InjectableFactory;
+use Espo\Core\Utils\Config;
+use Espo\Core\Utils\Event\Redis\RedisEventTransport;
/**
* @noinspection PhpUnused
@@ -39,10 +41,15 @@ class EventTransportLoader implements Loader
{
public function __construct(
private InjectableFactory $injectableFactory,
+ private Config $config,
) {}
public function load(): EventTransport
{
+ if ($this->config->get('eventTransport') === 'Redis') {
+ return $this->injectableFactory->create(RedisEventTransport::class);
+ }
+
return $this->injectableFactory->create(BypassEventTransport::class);
}
}
diff --git a/application/Espo/Core/Utils/Event/Redis/ClientProvider.php b/application/Espo/Core/Utils/Event/Redis/ClientProvider.php
new file mode 100644
index 0000000000..708847ce47
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/Redis/ClientProvider.php
@@ -0,0 +1,84 @@
+<?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\Event\Redis;
+
+use Espo\Core\Utils\Config;
+use Predis\Client;
+use Predis\CommunicationException;
+
+class ClientProvider
+{
+ private ?Client $client = null;
+
+ public function __construct(
+ private Config $config,
+ ) {}
+
+ public function get(): Client
+ {
+ if (!$this->client) {
+ $scheme = $this->config->get('redis.scheme') ?? null;
+ $port = $this->config->get('redis.port') ?? null;
+ $host = $this->config->get('redis.host') ?? null;
+
+ $params = [];
+
+ if ($scheme !== null) {
+ $params['scheme'] = $scheme;
+ }
+
+ if ($host !== null) {
+ $params['host'] = $host;
+ }
+
+ if ($port !== null) {
+ $params['port'] = $port;
+ }
+
+ $this->client = new Client($params);
+ }
+
+ return $this->client;
+ }
+
+ /**
+ * @noinspection PhpRedundantCatchClauseInspection
+ */
+ public function reconnect(): void
+ {
+ try {
+ $this->client?->disconnect();
+ } catch (CommunicationException) {}
+
+ $client = $this->get();
+
+ $client->connect();
+ }
+}
diff --git a/application/Espo/Core/Utils/Event/Redis/RedisEventTransport.php b/application/Espo/Core/Utils/Event/Redis/RedisEventTransport.php
new file mode 100644
index 0000000000..73d53d3da4
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/Redis/RedisEventTransport.php
@@ -0,0 +1,197 @@
+<?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\Event\Redis;
+
+use Closure;
+use Espo\Core\AclManager;
+use Espo\Core\Utils\Event\CrossInstanceEvent;
+use Espo\Core\Utils\Event\Envelope;
+use Espo\Core\Utils\Event\EventTransport;
+use Espo\Core\Utils\Json;
+use Espo\Core\Utils\Log;
+use LogicException;
+use Predis\Connection\ConnectionException;
+use RuntimeException;
+use stdClass;
+use Throwable;
+
+class RedisEventTransport implements EventTransport
+{
+ private const string STREAM_NAME = 'espocrm-events';
+
+ private const int COUNT = 50;
+
+ /**
+ * @var ?(Closure(Envelope): void) $callback
+ */
+ private ?Closure $callback = null;
+
+ private ?string $lastId = null;
+
+ public function __construct(
+ private ClientProvider $clientProvider,
+ private Log $log,
+ ) {}
+
+ public function subscribe(Closure $callback): void
+ {
+ $this->callback = $callback;
+ }
+
+ /**
+ * @noinspection PhpRedundantCatchClauseInspection
+ */
+ public function publish(Envelope $envelope): void
+ {
+ $json = Json::encode([
+ 'eventClassName' => $envelope->eventClassName,
+ 'payload' => $envelope->payload,
+ 'origin' => $envelope->origin,
+ ]);
+
+ try {
+ $this->publishRaw($json);
+ } catch (ConnectionException $e) {
+ $this->log->info("Redis connection lost, reconnecting.", ['exception' => $e]);
+
+ $this->clientProvider->reconnect();
+
+ $this->publishRaw($json);
+ }
+ }
+
+ public function tick(): void
+ {
+ $client = $this->clientProvider->get();
+
+ if ($this->lastId === null) {
+ $response = $client->xread(1, null, [self::STREAM_NAME], '+');
+
+ $this->lastId = '0';
+
+ foreach (($response[self::STREAM_NAME] ?? []) as $item) {
+ [$messageId] = $this->getMessageData($item);
+
+ $this->lastId = $messageId;
+
+ break;
+ }
+ }
+
+ $response = $client->xread(self::COUNT, null, [self::STREAM_NAME], $this->lastId);
+
+ $jsonItems = [];
+
+ foreach (($response[self::STREAM_NAME] ?? []) as $item) {
+ [$messageId, $json] = $this->getMessageData($item);
+
+ $this->lastId = $messageId;
+
+ $jsonItems[] = $json;
+ }
+
+ if (!$this->callback) {
+ return;
+ }
+
+ foreach ($jsonItems as $json) {
+ $this->processMessageItem($json);
+ }
+ }
+
+ private function processMessageItem(string $json): void
+ {
+ if (!$this->callback) {
+ throw new LogicException();
+ }
+
+ $data = Json::decode($json);
+
+ /** @var ?class-string<CrossInstanceEvent> $eventClassName */
+ $eventClassName = $data->eventClassName ?? null;
+ $payload = $data->payload ?? null;
+ $origin = $data->origin ?? null;
+
+ if (!is_string($eventClassName)) {
+ throw new RuntimeException();
+ }
+
+ if (!$payload instanceof stdClass) {
+ throw new RuntimeException();
+ }
+
+ if (!is_string($origin)) {
+ throw new RuntimeException();
+ }
+
+ $envelope = new Envelope(
+ eventClassName: $eventClassName,
+ payload: $payload,
+ origin: $origin,
+ );
+
+ try {
+ ($this->callback)($envelope);
+ } catch (Throwable $e) {
+ $this->log->error("Event callback error, {eventClassName}.", [
+ 'exception' => $e,
+ 'eventClassName' => $eventClassName,
+ 'origin' => $origin,
+ ]);
+ }
+ }
+
+ private function publishRaw(string $json): void
+ {
+ $client = $this->clientProvider->get();
+
+ $client->xadd(self::STREAM_NAME, ['data' => $json]);
+ }
+
+ /**
+ * @param array<int, mixed> $item
+ * @return array{string, string}
+ */
+ private function getMessageData(mixed $item): array
+ {
+ $messageId = $item[0] ?? null;
+ $json = $item[1][1] ?? null;
+
+ if ($messageId === null) {
+ throw new RuntimeException("Bad message data, no ID.");
+ }
+
+ if (!is_string($json)) {
+ throw new RuntimeException("Bad message data.");
+ }
+
+ return [$messageId, $json];
+ }
+}
diff --git a/application/Espo/Resources/defaults/systemConfig.php b/application/Espo/Resources/defaults/systemConfig.php
index 348b336bcc..06b9d890b6 100644
--- a/application/Espo/Resources/defaults/systemConfig.php
+++ b/application/Espo/Resources/defaults/systemConfig.php
@@ -121,6 +121,8 @@ return [
'cleanupAppLog',
'cleanupAppLogPeriod',
'rabbitMq',
+ 'redis',
+ 'eventTransport',
],
'adminItems' => [
'devMode',
diff --git a/composer.json b/composer.json
index dce4186af1..69fddaf364 100644
--- a/composer.json
+++ b/composer.json
@@ -57,7 +57,8 @@
"zbateson/mail-mime-parser": "^3.0",
"guzzlehttp/guzzle": "^7.10",
"devtheorem/php-handlebars": "^1.0",
- "php-amqplib/php-amqplib": "^3.7"
+ "php-amqplib/php-amqplib": "^3.7",
+ "predis/predis": "^3.5"
},
"require-dev": {
"phpunit/phpunit": "^11.5",
diff --git a/composer.lock b/composer.lock
index 7a18c8f8e6..a7d45b5359 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": "e8dd3b2f9887137751d0a383c4e27ce5",
+ "content-hash": "1ae60dc55489013607247b563b8935f3",
"packages": [
{
"name": "async-aws/core",
@@ -4499,6 +4499,69 @@
},
"time": "2026-01-08T08:57:40+00:00"
},
+ {
+ "name": "predis/predis",
+ "version": "v3.5.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/predis/predis.git",
+ "reference": "5c996db191ee2d9bafe651f454b1fca16754271b"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/predis/predis/zipball/5c996db191ee2d9bafe651f454b1fca16754271b",
+ "reference": "5c996db191ee2d9bafe651f454b1fca16754271b",
+ "shasum": ""
+ },
+ "require": {
+ "php": "^7.2 || ^8.0",
+ "psr/http-message": "^1.0|^2.0"
+ },
+ "require-dev": {
+ "friendsofphp/php-cs-fixer": "^3.3",
+ "phpstan/phpstan": "^1.9",
+ "phpunit/phpcov": "^6.0 || ^8.0",
+ "phpunit/phpunit": "^8.0 || ~9.4.4"
+ },
+ "suggest": {
+ "ext-relay": "Faster connection with in-memory caching (>=0.6.2)"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "Predis\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Till Krüss",
+ "homepage": "https://till.im",
+ "role": "Maintainer"
+ }
+ ],
+ "description": "A flexible and feature-complete Redis/Valkey client for PHP.",
+ "homepage": "http://github.com/predis/predis",
+ "keywords": [
+ "nosql",
+ "predis",
+ "redis"
+ ],
+ "support": {
+ "issues": "https://github.com/predis/predis/issues",
+ "source": "https://github.com/predis/predis/tree/v3.5.1"
+ },
+ "funding": [
+ {
+ "url": "https://github.com/sponsors/tillkruss",
+ "type": "github"
+ }
+ ],
+ "time": "2026-06-11T16:56:53+00:00"
+ },
{
"name": "psr/cache",
"version": "3.0.0",