-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
154 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,62 @@ | ||
import { Redis } from "ioredis" | ||
import { version } from "ioredis/package.json" | ||
import logger from "@/globals/logger" | ||
import env from "@/globals/env" | ||
import { time } from "@rjweb/utils" | ||
|
||
const localCache = new Map<string, any>() | ||
const startTime = performance.now(), | ||
localCache = new Map<string, any>() | ||
|
||
export default { | ||
const redis = env.REDIS_MODE === 'redis' | ||
? new Redis(env.REDIS_URL) | ||
: new Redis({ | ||
sentinels: env.REDIS_SENTINEL_NODES.map(([host, port]) => ({ host, port })), | ||
name: 'mymaster' | ||
}) | ||
|
||
redis.once('connect', () => { | ||
logger() | ||
.text('Cache', (c) => c.yellow) | ||
.text(`(${version}) Connection established!`) | ||
.text(`(${(performance.now() - startTime).toFixed(1)}ms)`, (c) => c.gray) | ||
.info() | ||
}) | ||
|
||
export default Object.assign(redis, { | ||
async use<Run extends () => Promise<any> | any>(key: string, run: Run, expire: number = time(3).s()): Promise<Awaited<ReturnType<Run>>> { | ||
const mapResult = localCache.get(`internal-middlewares::cache::${key}`) | ||
if (mapResult) return mapResult | ||
|
||
const redisResult = await redis.get(`internal-middlewares::cache::${key}`) | ||
if (redisResult) return JSON.parse(redisResult) | ||
|
||
const runResult = await Promise.resolve(run()) | ||
localCache.set(`internal-middlewares::cache::${key}`, runResult) | ||
if (!expire) await redis.set(`internal-middlewares::cache::${key}`, JSON.stringify(runResult)) | ||
else if (expire > time(15).s()) await redis.set(`internal-middlewares::cache::${key}`, JSON.stringify(runResult), 'EX', Math.ceil(expire / 1000)) | ||
else { | ||
localCache.set(`internal-middlewares::cache::${key}`, runResult) | ||
|
||
setTimeout(() => { | ||
localCache.delete(`internal-middlewares::cache::${key}`) | ||
}, expire) | ||
setTimeout(() => { | ||
localCache.delete(`internal-middlewares::cache::${key}`) | ||
}, expire) | ||
} | ||
|
||
return runResult | ||
} | ||
} | ||
}, | ||
|
||
local: { | ||
use<Run extends () => any>(key: string, run: Run, expire: number = time(3).s()): ReturnType<Run> { | ||
const mapResult = localCache.get(`internal-middlewares::cache::${key}`) | ||
if (mapResult) return mapResult | ||
|
||
const runResult = run() | ||
localCache.set(`internal-middlewares::cache::${key}`, runResult) | ||
|
||
if (!!expire) setTimeout(() => { | ||
localCache.delete(`internal-middlewares::cache::${key}`) | ||
}, expire) | ||
|
||
return runResult | ||
} | ||
} as const | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters