-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0b5afa
commit da74622
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- Key for the ZSET storing price data | ||
local priceCacheKey = KEYS[1] | ||
-- Price to be added | ||
local price = tonumber(ARGV[1]) | ||
-- Current timestamp | ||
local nowSeconds = tonumber(ARGV[2]) | ||
-- Time window (5 seconds) | ||
local fiveSeconds = 5 | ||
|
||
-- 1. Add the price to the sorted set (score is the current timestamp) | ||
redis.call("zadd", priceCacheKey, nowSeconds, price) | ||
|
||
-- 2. Remove any entries older than 5 seconds | ||
local cutoffTime = nowSeconds - fiveSeconds | ||
redis.call("zremrangebyscore", priceCacheKey, "-inf", cutoffTime) | ||
|
||
return true |
23 changes: 23 additions & 0 deletions
23
indexer/packages/redis/src/scripts/get_market_median_price.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- Key for the sorted set storing price data | ||
local priceCacheKey = KEYS[1] | ||
|
||
-- Get all the prices from the sorted set (ascending order) | ||
local prices = redis.call('zrange', priceCacheKey, 0, -1) | ||
|
||
-- If no prices are found, return nil | ||
if #prices == 0 then | ||
return nil | ||
end | ||
|
||
-- Calculate the middle index | ||
local middle = math.floor(#prices / 2) | ||
|
||
-- Calculate median | ||
if #prices % 2 == 0 then | ||
-- If even, return the average of the two middle elements | ||
local median = (tonumber(prices[middle]) + tonumber(prices[middle + 1])) / 2 | ||
return tostring(median) | ||
else | ||
-- If odd, return the middle element | ||
return prices[middle + 1] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters