added wss tools 9

This commit is contained in:
2026-06-18 20:05:31 +03:30
parent 21341c7f13
commit 97245a9b81
3 changed files with 53 additions and 40 deletions
+30 -5
View File
@@ -12,6 +12,10 @@ class PriceBroadcastService
private const LIST_HASH_KEY = 'price_broadcast:list_hash';
private const LIST_SNAPSHOT_KEY = 'price_broadcast:list_snapshot';
private const LIST_SNAPSHOT_SECONDS = 8;
/**
* برادکست SSE — payload کامل last/list وقتی داده عوض شود.
*
@@ -90,9 +94,29 @@ class PriceBroadcastService
$this->publishListIfChanged(true);
}
public function buildListPayload(): ?array
public function buildListPayload(bool $allowCached = false): ?array
{
return $this->buildProductListPayload();
if ($allowCached) {
$cached = Cache::get(self::LIST_SNAPSHOT_KEY);
if (
is_array($cached)
&& is_array($cached['body'] ?? null)
&& isset($cached['stored_at'])
&& now()->diffInSeconds($cached['stored_at']) <= self::LIST_SNAPSHOT_SECONDS
) {
return $cached['body'];
}
}
$body = $this->buildProductListPayload();
if ($body !== null && $body !== []) {
Cache::put(self::LIST_SNAPSHOT_KEY, [
'body' => $body,
'stored_at' => now(),
], now()->addSeconds(self::LIST_SNAPSHOT_SECONDS + 5));
}
return $body;
}
public function broadcastListIfChanged(bool $force = false): bool
@@ -112,14 +136,15 @@ class PriceBroadcastService
return false;
}
if (! $force && ! $this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload))) {
if (! $force && ! $this->rememberHashIfChanged(self::LIST_HASH_KEY, self::listIdentity($listPayload))) {
return false;
}
if ($force) {
$this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload));
$this->rememberHashIfChanged(self::LIST_HASH_KEY, self::listIdentity($listPayload));
}
Cache::forget(self::LIST_SNAPSHOT_KEY);
app(PriceStreamService::class)->publish('list', $listPayload);
return true;
@@ -129,7 +154,7 @@ class PriceBroadcastService
* @param array<int, array<string, mixed>> $listPayload
* @return array<int, array<string, int|bool>>
*/
private function listIdentity(array $listPayload): array
public static function listIdentity(array $listPayload): array
{
$identity = [];