REDIS config + flatten events

This commit is contained in:
Evann Regnault 2022-07-29 22:26:06 +02:00
parent 455ae8c0ee
commit 2a31aebb00
2 changed files with 11 additions and 7 deletions

View file

@ -1,9 +1,11 @@
import {getEvents} from "./utils"; import {getEvents} from "./utils";
import {flatten} from "next/dist/shared/lib/flatten";
export default async function handler(req, res) { export default async function handler(req, res) {
const events = await getEvents(); const events = await getEvents();
if (events) { if (events) {
res.status(200).json(events); console.log(flatten(events))
res.status(200).json(flatten(events));
} else { } else {
res.status(404).json({message: "An error has occured"}); res.status(404).json({message: "An error has occured"});
} }

View file

@ -1,9 +1,11 @@
const ENABLE_REDIS = true;
const redis = require("redis"); const redis = require("redis");
const client = redis.createClient("redis://localhost:6379"); const client = ENABLE_REDIS ? redis.createClient("redis://localhost:6379") : null;
client.connect(); client?.connect();
export async function getResultFromRedis(key){ export async function getResultFromRedis(key){
if (ENABLE_REDIS)
if (await client.exists(key)) { if (await client.exists(key)) {
return await client.get(key) return await client.get(key)
} }
@ -11,5 +13,5 @@ export async function getResultFromRedis(key){
} }
export async function addResultToRedis(key, value){ export async function addResultToRedis(key, value){
await client.set(key, value, {EX: 600}); if (ENABLE_REDIS) await client.set(key, value, {EX: 600});
} }