86 lines
2.0 KiB
JavaScript
86 lines
2.0 KiB
JavaScript
import {createClient} from "redis";
|
|
|
|
export const redis = createClient({
|
|
url: process.env.DEXORDER_REDIS_URL || 'redis://localhost:6379',
|
|
returnBuffers: false,
|
|
})
|
|
|
|
redis.on('error', (err) => console.log('Redis Client Error', err));
|
|
await redis.connect();
|
|
|
|
|
|
export class CacheSet {
|
|
|
|
constructor(series) {
|
|
this.series = series
|
|
}
|
|
|
|
async contains(chain, key) {
|
|
const result = await redis.sIsMember(`${chain}|${this.series}`, key)
|
|
console.log('contains', `${chain}|${this.series}`, key, result)
|
|
return result
|
|
}
|
|
|
|
}
|
|
|
|
|
|
export class CacheDict {
|
|
constructor(series) {
|
|
this.series = series
|
|
}
|
|
|
|
async get(chain, key, default_=null) {
|
|
const result = await redis.hGet(`${chain}|${this.series}`, key)
|
|
return result === null ? default_ : '' + result
|
|
}
|
|
|
|
async contains(chain, key) {
|
|
return await redis.hExists(`${chain}|${this.series}`, key)
|
|
}
|
|
}
|
|
|
|
|
|
export class CacheDictObject {
|
|
constructor(series) {
|
|
this.series = series
|
|
}
|
|
|
|
async get(chain, key) {
|
|
return await redis.json.get(`${chain}|${this.series}`, key)
|
|
}
|
|
|
|
async contains(chain, key) {
|
|
return await redis.json.get(`${chain}|${this.series}`, key) !== null
|
|
}
|
|
}
|
|
|
|
|
|
export class CacheObject {
|
|
constructor(series) {
|
|
this.series = series
|
|
}
|
|
|
|
async get(chain) {
|
|
return await redis.json.get(`${chain}|${this.series}`)
|
|
}
|
|
}
|
|
|
|
const blockCaches = {
|
|
'31337': new CacheObject('31337|latest_block'),
|
|
'42161': new CacheObject('42161|latest_block'),
|
|
'1337': new CacheObject('1337|latest_block'),
|
|
}
|
|
|
|
async function latestBlock(chain) {
|
|
return await blockCaches[chain].get()
|
|
}
|
|
|
|
export const vaults = new CacheDict('v')
|
|
export const vaultBalances = new CacheDict('vb')
|
|
export const prices = new CacheDict('p')
|
|
export const orderStatuses = new CacheDict('o')
|
|
export const vaultOpenOrders = new CacheDict('voo')
|
|
export const vaultRecentlyClosedOrders = new CacheDict('vrco')
|
|
export const orderFilled = new CacheDict('of')
|
|
export const ohlcs = new CacheDict('ohlc')
|