fix price
This commit is contained in:
@@ -129,7 +129,10 @@ class ReportController extends Controller
|
|||||||
app(ProductDisplayPriceService::class)->cacheLivePricePayload($payload);
|
app(ProductDisplayPriceService::class)->cacheLivePricePayload($payload);
|
||||||
|
|
||||||
if ($saveFullCache || request()->boolean('force_refresh')) {
|
if ($saveFullCache || request()->boolean('force_refresh')) {
|
||||||
return app(LivePriceCacheService::class)->put($payload);
|
$cache = app(LivePriceCacheService::class);
|
||||||
|
$cache->markTick();
|
||||||
|
|
||||||
|
return $cache->put($payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $payload;
|
return $payload;
|
||||||
@@ -147,9 +150,12 @@ class ReportController extends Controller
|
|||||||
return $cached;
|
return $cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
$dbBuilt = $cache->buildFromDatabase();
|
// فقط وقتی کران اخیراً tick زده — وگرنه API زنده (جلوگیری از قفل قیمت)
|
||||||
if (($dbBuilt['price'] ?? 0) > 0) {
|
if ($cache->hasRecentTick()) {
|
||||||
return $cache->put($dbBuilt);
|
$dbBuilt = $cache->buildFromDatabase();
|
||||||
|
if (($dbBuilt['price'] ?? 0) > 0) {
|
||||||
|
return $cache->put($dbBuilt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,13 @@ class LivePriceCacheService
|
|||||||
{
|
{
|
||||||
public const FULL_RESPONSE_KEY = 'live_price_full_response';
|
public const FULL_RESPONSE_KEY = 'live_price_full_response';
|
||||||
|
|
||||||
public const TTL_SECONDS = 15;
|
public const LAST_TICK_KEY = 'live_price_last_tick_at';
|
||||||
|
|
||||||
|
/** همتراز با کران ۱۰ ثانیهای */
|
||||||
|
public const TTL_SECONDS = 10;
|
||||||
|
|
||||||
|
/** اگر tick اخیر نباشد، buildFromDatabase قیمت را قفل میکند */
|
||||||
|
public const TICK_MAX_AGE_SECONDS = 20;
|
||||||
|
|
||||||
public function get(): ?array
|
public function get(): ?array
|
||||||
{
|
{
|
||||||
@@ -37,7 +43,22 @@ class LivePriceCacheService
|
|||||||
return $body;
|
return $body;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** پاسخ سریع بدون API خارجی — از آخرین رکورد prices + تنظیمات (همان فرمول lastPrice) */
|
public function markTick(): void
|
||||||
|
{
|
||||||
|
Cache::put(self::LAST_TICK_KEY, now(), now()->addMinutes(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasRecentTick(): bool
|
||||||
|
{
|
||||||
|
$lastTick = Cache::get(self::LAST_TICK_KEY);
|
||||||
|
if ($lastTick === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return now()->diffInSeconds($lastTick) <= self::TICK_MAX_AGE_SECONDS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** پاسخ سریع بدون API خارجی — فقط وقتی کران اخیراً tick زده (همان فرمول lastPrice) */
|
||||||
public function buildFromDatabase(): array
|
public function buildFromDatabase(): array
|
||||||
{
|
{
|
||||||
$config = app(ConfigService::class);
|
$config = app(ConfigService::class);
|
||||||
@@ -66,8 +87,10 @@ class LivePriceCacheService
|
|||||||
|
|
||||||
if ($provider === 'montzar') {
|
if ($provider === 'montzar') {
|
||||||
$liveMontzar = (float) $config->get('montzar_live_price', 0);
|
$liveMontzar = (float) $config->get('montzar_live_price', 0);
|
||||||
|
if ($liveMontzar > 0) {
|
||||||
|
$base = $liveMontzar;
|
||||||
|
}
|
||||||
$montzarSource = (int) ($liveMontzar > 0 ? $liveMontzar : $base);
|
$montzarSource = (int) ($liveMontzar > 0 ? $liveMontzar : $base);
|
||||||
// DB از آخرین tick montzar پر شده — همان فرمول نمایش معامله
|
|
||||||
$priceSource = 'montzar';
|
$priceSource = 'montzar';
|
||||||
} elseif ($provider === 'zaarbox') {
|
} elseif ($provider === 'zaarbox') {
|
||||||
$zaarboxSource = (int) $base;
|
$zaarboxSource = (int) $base;
|
||||||
|
|||||||
@@ -76,6 +76,8 @@ class LivePriceTickService
|
|||||||
if ($payload !== []) {
|
if ($payload !== []) {
|
||||||
app(PriceSnapshotService::class)->recordTick($payload);
|
app(PriceSnapshotService::class)->recordTick($payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app(LivePriceCacheService::class)->markTick();
|
||||||
} finally {
|
} finally {
|
||||||
$lock->release();
|
$lock->release();
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,7 @@ use App\Http\Controllers\Admin\CategoryController;
|
|||||||
|
|
||||||
Route::get('/candle',[ReportController::class, 'candleChart']);
|
Route::get('/candle',[ReportController::class, 'candleChart']);
|
||||||
Route::get('/price/up',[ReportController::class, 'upDownPrice']);
|
Route::get('/price/up',[ReportController::class, 'upDownPrice']);
|
||||||
Route::get('/price/last',[ReportController::class, 'lastPrice']);
|
Route::match(['get', 'post'],'/price/last',[ReportController::class, 'lastPrice']);
|
||||||
Route::get('/price/updatePrise',[ReportController::class, 'updatePrise']);
|
Route::get('/price/updatePrise',[ReportController::class, 'updatePrise']);
|
||||||
Route::get('/const/key/{key}',[ConstController::class, 'showValue']);
|
Route::get('/const/key/{key}',[ConstController::class, 'showValue']);
|
||||||
Route::get('/check',[ReportController::class, 'check']);
|
Route::get('/check',[ReportController::class, 'check']);
|
||||||
|
|||||||
Reference in New Issue
Block a user