server works with backend via redis, serves chainInfo, more

This commit is contained in:
Tim Olson
2023-10-04 03:42:21 -04:00
parent e36f7812eb
commit c05c9b9c16
12 changed files with 404 additions and 209 deletions

View File

@@ -1,19 +1,53 @@
// implement a cluster-wide cache which is in-memory for each instance
import {createClient} from "redis";
export class Cache {
export const redis = createClient({url: process.env.DEXORDER_REDIS_URL || 'redis://localhost:6379'})
constructor(name) {
this.name = name
this.cache = {}
redis.on('error', (err) => console.log('Redis Client Error', err));
await redis.connect();
export class CacheSet {
constructor(series) {
this.series = series
}
async get(key) {
return this.cache[key]
async contains(chain, key) {
return await redis.sIsMember(`${chain}|${this.series}`, key)
}
async set(key, value) {
this.cache[key] = value
// todo broadcast
}
export class CacheDict {
constructor(series) {
this.series = series
}
async get(chain, key) {
return await redis.hGet(`${chain}|${this.series}`, key)
}
}
export class CacheObject {
constructor(series) {
this.series = series
}
async get(chain) {
return await redis.json.get(`${chain}|${this.series}`)
}
}
const cache_blocks = {
'1338': new CacheObject('1338|latest_block'),
}
async function latestBlock(chain) {
return await cache_blocks[chain].get()
}
export const vaults = new CacheSet('v')
export const vaultTokens = new CacheDict('vt')
export const prices = new CacheDict('p')