Skip to content

Commit

Permalink
fixed bugs on serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ARtheboss committed Oct 24, 2023
1 parent 06e5fce commit b078156
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions backend/src/redis/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RedisClientType, createClient } from "redis";
import { config } from "../config";
import { performance } from "perf_hooks";

const REDIS_FIELD_DELIMITER = "@@";
const REDIS_FIELD_DELIMITER = "@@"

type RedisStoreFormat = {
isArray: boolean,
Expand All @@ -11,24 +12,25 @@ type RedisStoreFormat = {
}

class RedisConnection {
client: RedisClientType|null = null;
client: RedisClientType|null = null

async load() {
this.client = createClient({ url: config.REDIS_URI });
await this.client.connect();
this.client.on('error', (err) => console.log('Redis Client Error', err));
this.client = createClient({ url: config.REDIS_URI })
await this.client.connect()
this.client.on('error', (err) => console.log('Redis Client Error', err))
};
isActive() : boolean {
return this.client != null;
return this.client != null
}
quit() {
this.client?.quit();
this.client?.quit()
}

async get(controller:string, parameters:string[]) : Promise<any> {
if(!this.client) return null

const res = await this.client.get(`${controller}/${parameters.join("/")}`)

if(res){
// cache hit
const schema:RedisStoreFormat = JSON.parse(res)
Expand All @@ -41,7 +43,7 @@ class RedisConnection {
if(k == "lastUpdated") return new Date(v) // TO DO: other date-like fields?
return v
}))
return schema.fields.reduce((map, val:string, index:number) => {
return schema.fields.reduce((map, val:string, index:number) => {
return {
...map,
[val]: valuesParsed[index],
Expand All @@ -51,56 +53,54 @@ class RedisConnection {
if (!schema.isArray) return parsedData[0]
return parsedData
}
return null;
return null
}

set(controller:string, parameters:string[], data:any) {
if(!this.client) return;
if(!this.client) return

// convert non-arrays to array for data field and remember for .get
const isArray = Array.isArray(data)
if (!isArray) data = [data]

// Object fields need to be JSON.stringified
const indexJsonFields:number[] = []; // saved for .get
const nameJsonFields:string[] = []; // for processing
const jsonFields:any[] = [] // saved for .get, [index, name]
Object.keys(data[0]).forEach((k:string, index:number) => {
if (data[0][k].toString == Object.prototype.toString) {
nameJsonFields.push(k)
indexJsonFields.push(index)
if (data[0][k] instanceof Object && !(data[0][k] instanceof Date)) {
jsonFields.push([index, k])
}
});
})

//
const schemaData = data.map((val:any) => {
const valuesList = Object.values(val)
// store each item in array as a object with only values
nameJsonFields.forEach((k:string) => {
jsonFields.forEach((t:any[]) => {
// stringify each Object fields
val[k] = JSON.stringify(val[k])
valuesList[t[0]] = JSON.stringify(val[t[1]])
})
return Object.values(val).join(REDIS_FIELD_DELIMITER)
return valuesList.join(REDIS_FIELD_DELIMITER)
})


const schema : RedisStoreFormat = {
isArray: isArray,
fields: Object.keys(data[0]),
jsonFields: indexJsonFields,
jsonFields: jsonFields.map((t:any[]) => t[0]),
data: schemaData,
}

this.client.set(`${controller}/${parameters.join("/")}`, JSON.stringify(schema))
}
}

const redis = new RedisConnection();
const redis = new RedisConnection()

async function cache(controller : Function, ...args: any[]) {

const cacheData = await redis.get("getCatalog", args.map((v) => JSON.stringify(v)))
if(cacheData){
// cache hit
return cacheData;
return cacheData
}

const resp = await controller(...args)
Expand All @@ -109,5 +109,5 @@ async function cache(controller : Function, ...args: any[]) {

}

export { redis, cache };
export { redis, cache }

0 comments on commit b078156

Please sign in to comment.