Skip to content

Commit f34c2df

Browse files
adamfrasermergify[bot]
authored andcommitted
Increase orderbook mid price cache duration to 60 seconds (#2633)
(cherry picked from commit 7da33a6)
1 parent 87a43cd commit f34c2df

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

indexer/packages/redis/__tests__/caches/orderbook-mid-prices-cache.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('orderbook-mid-prices-cache', () => {
136136
expect(result).toEqual({ 'BTC-USD': '50500' });
137137
});
138138

139-
it('returns the correct median price after 30 seconds', async () => {
139+
it('returns the correct median price after 60 seconds', async () => {
140140
jest.useFakeTimers();
141141
// Mock the getOrderBookMidPrice function for the ticker
142142
const mockPrices: string[] = ['50000', '51000', '49000', '48000', '52000', '53000'];
@@ -154,7 +154,7 @@ describe('orderbook-mid-prices-cache', () => {
154154
expect(OrderbookLevelsCache.getOrderBookMidPrice).toHaveBeenCalledTimes(2);
155155

156156
// Advance time and fetch more prices
157-
jest.advanceTimersByTime(31000); // Advance time by 31 seconds
157+
jest.advanceTimersByTime(61000); // Advance time by 61 seconds
158158
await fetchAndCacheOrderbookMidPrices(
159159
client,
160160
[defaultTicker, defaultTicker, defaultTicker, defaultTicker],

indexer/packages/redis/src/scripts/add_orderbook_mid_prices.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ local numArgs = #ARGV
77
-- Get the timestamp from the last argument
88
local timestamp = tonumber(ARGV[numArgs])
99

10-
-- Time window (30 seconds)
11-
local thirtySeconds = 30
10+
-- Time window (60 seconds)
11+
local sixtySeconds = 60
1212

1313
-- Validate the timestamp
1414
if not timestamp then
1515
return redis.error_reply("Invalid timestamp")
1616
end
1717

1818
-- Calculate the cutoff time for removing old prices
19-
local cutoffTime = timestamp - thirtySeconds
19+
local cutoffTime = timestamp - sixtySeconds
2020

2121
-- Iterate through each key (market) and corresponding price
2222
for i = 1, numKeys do
@@ -31,7 +31,7 @@ for i = 1, numKeys do
3131
-- Add the price to the sorted set with the current timestamp as the score
3232
redis.call("ZADD", priceCacheKey, timestamp, price)
3333

34-
-- Remove entries older than the cutoff time (older than 30 seconds)
34+
-- Remove entries older than the cutoff time (older than 60 seconds)
3535
redis.call("ZREMRANGEBYSCORE", priceCacheKey, "-inf", cutoffTime)
3636
end
3737

0 commit comments

Comments
 (0)