Initial conversion to react-basics.

This commit is contained in:
Mike Cao 2022-11-21 22:32:55 -08:00
parent c0a18e13fa
commit 2259ee8d76
10 changed files with 724 additions and 609 deletions

View file

@ -1,39 +1,39 @@
import { createClient } from 'redis';
import debug from 'debug';
import Redis from 'ioredis';
import { REDIS } from 'lib/db';
const log = debug('umami:redis');
export const DELETED = 'deleted';
const REDIS = Symbol();
let redis;
const enabled = Boolean(process.env.REDIS_URL);
function getClient() {
if (!enabled) {
async function getClient() {
if (!process.env.REDIS_URL) {
return null;
}
const redis = new Redis(process.env.REDIS_URL, {
retryStrategy(times) {
log(`Redis reconnecting attempt: ${times}`);
return 5000;
},
});
const client = createClient({ url: process.env.REDIS_URL });
client.on('error', err => log(err));
await client.connect();
if (process.env.NODE_ENV !== 'production') {
global[REDIS] = redis;
global[REDIS] = client;
}
log('Redis initialized');
return redis;
return client;
}
async function get(key) {
await connect();
const data = await redis.get(key);
log({ key, data });
try {
return JSON.parse(await redis.get(key));
return JSON.parse(data);
} catch {
return null;
}
@ -53,7 +53,7 @@ async function del(key) {
async function connect() {
if (!redis && enabled) {
redis = global[REDIS] || getClient();
redis = global[REDIS] || (await getClient());
}
return redis;