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
@@ -45,7 +45,7 @@ class PriceStreamController extends Controller
$send('last', $last);
}
$list = app(PriceBroadcastService::class)->buildListPayload();
$list = app(PriceBroadcastService::class)->buildListPayload(true);
if (is_array($list) && $list !== []) {
$send('list', $list);
}
+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 = [];
+22 -34
View File
@@ -99,28 +99,31 @@ class PriceStreamService
}
$key = $this->inboxKey();
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
$events = is_array($inbox['events'] ?? null) ? $inbox['events'] : [];
$lastEvent = $events !== [] ? $events[array_key_last($events)] : null;
if (is_array($lastEvent)
&& ($lastEvent['event'] ?? null) === $event
&& $this->eventDataHash($event, $lastEvent['data'] ?? null) === $this->eventDataHash($event, $data)) {
return;
}
Cache::lock($key.':lock', 5)->block(3, function () use ($key, $event, $data): void {
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
$events = is_array($inbox['events'] ?? null) ? $inbox['events'] : [];
$lastEvent = $events !== [] ? $events[array_key_last($events)] : null;
$inbox['seq'] = (int) ($inbox['seq'] ?? 0) + 1;
$inbox['events'][] = [
'id' => $inbox['seq'],
'event' => $event,
'data' => $data,
];
if (is_array($lastEvent)
&& ($lastEvent['event'] ?? null) === $event
&& $this->eventDataHash($event, $lastEvent['data'] ?? null) === $this->eventDataHash($event, $data)) {
return;
}
if (count($inbox['events']) > self::MAX_EVENTS) {
$inbox['events'] = array_slice($inbox['events'], -self::MAX_EVENTS);
}
$inbox['seq'] = (int) ($inbox['seq'] ?? 0) + 1;
$inbox['events'][] = [
'id' => $inbox['seq'],
'event' => $event,
'data' => $data,
];
Cache::put($key, $inbox, now()->addSeconds(self::INBOX_TTL_SECONDS));
if (count($inbox['events']) > self::MAX_EVENTS) {
$inbox['events'] = array_slice($inbox['events'], -self::MAX_EVENTS);
}
Cache::put($key, $inbox, now()->addSeconds(self::INBOX_TTL_SECONDS));
});
}
private function inboxKey(): string
@@ -142,22 +145,7 @@ class PriceStreamService
}
if ($event === 'list') {
$identity = [];
foreach ($data as $product) {
if (! is_array($product)) {
continue;
}
$identity[] = [
'id' => (int) ($product['id'] ?? 0),
'price' => (int) ($product['price'] ?? 0),
'price_profit' => (int) ($product['price_profit'] ?? 0),
'show' => (bool) ($product['show'] ?? true),
'active' => (bool) ($product['active'] ?? true),
];
}
return md5(json_encode($identity, JSON_UNESCAPED_UNICODE));
return md5(json_encode(PriceBroadcastService::listIdentity($data), JSON_UNESCAPED_UNICODE));
}
return md5(json_encode($data, JSON_UNESCAPED_UNICODE));