small redis optimizations
This commit is contained in:
parent
34f19a7e00
commit
3ceeda1b3c
1 changed files with 68 additions and 15 deletions
|
@ -1,22 +1,75 @@
|
||||||
import Redis from "ioredis";
|
import Redis from "ioredis";
|
||||||
|
|
||||||
export const cache = process.env.REDIS_URL
|
let cache: Redis | null = null;
|
||||||
? new Redis(process.env.REDIS_URL, {
|
let pub: Redis | null = null;
|
||||||
family: 6,
|
let sub: Redis | null = null;
|
||||||
})
|
|
||||||
: null;
|
|
||||||
|
|
||||||
export const pub = process.env.REDIS_URL
|
declare global {
|
||||||
? new Redis(process.env.REDIS_URL, {
|
var __cache: Redis | null;
|
||||||
family: 6,
|
var __pub: Redis | null;
|
||||||
})
|
var __sub: Redis | null;
|
||||||
: null;
|
}
|
||||||
|
|
||||||
export const sub = process.env.REDIS_URL
|
if (process.env.NODE_ENV === "production") {
|
||||||
|
cache = process.env.REDIS_URL
|
||||||
? new Redis(process.env.REDIS_URL, {
|
? new Redis(process.env.REDIS_URL, {
|
||||||
family: 6,
|
family: 6,
|
||||||
})
|
})
|
||||||
: null;
|
: null;
|
||||||
|
} else {
|
||||||
|
if (!global.__cache) {
|
||||||
|
global.__cache = process.env.REDIS_URL
|
||||||
|
? new Redis(process.env.REDIS_URL, {
|
||||||
|
family: 6,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
cache = global.__cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === "production") {
|
||||||
|
pub = process.env.REDIS_URL
|
||||||
|
? new Redis(process.env.REDIS_URL, {
|
||||||
|
family: 6,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
} else {
|
||||||
|
if (!global.__pub) {
|
||||||
|
global.__pub = process.env.REDIS_URL
|
||||||
|
? new Redis(process.env.REDIS_URL, {
|
||||||
|
family: 6,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
pub = global.__pub;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === "production") {
|
||||||
|
sub = process.env.REDIS_URL
|
||||||
|
? new Redis(process.env.REDIS_URL, {
|
||||||
|
family: 6,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
} else {
|
||||||
|
if (!global.__sub) {
|
||||||
|
global.__sub = process.env.REDIS_URL
|
||||||
|
? new Redis(process.env.REDIS_URL, {
|
||||||
|
family: 6,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
sub = global.__sub;
|
||||||
|
}
|
||||||
|
|
||||||
|
// export const isConnectedToCache = process.env.REDIS_URL
|
||||||
|
// ? cache?.status === "ready"
|
||||||
|
// : true;
|
||||||
|
// export const isConnectedToPub = process.env.REDIS_URL
|
||||||
|
// ? pub?.status === "ready"
|
||||||
|
// : true;
|
||||||
|
// export const isConnectedToSub = process.env.REDIS_URL
|
||||||
|
// ? sub?.status === "ready"
|
||||||
|
// : true;
|
||||||
|
|
||||||
export const publishToChannel = async (channel: string, message: string) => {
|
export const publishToChannel = async (channel: string, message: string) => {
|
||||||
await pub?.publish(channel, JSON.stringify(message));
|
await pub?.publish(channel, JSON.stringify(message));
|
||||||
|
|
Loading…
Add table
Reference in a new issue