server works with backend via redis, serves chainInfo, more
This commit is contained in:
54
cache.js
54
cache.js
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user