Skip to content

Commit 0d56f88

Browse files
committed
refactor: CacheManager
1 parent b092bb2 commit 0d56f88

File tree

4 files changed

+58
-49
lines changed

4 files changed

+58
-49
lines changed

src/cache.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +0,0 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { logger } from '.'
3-
import sha256 from 'crypto-js/sha256'
4-
import { appendFile, readFile } from 'fs/promises'
5-
import { join } from 'path'
6-
7-
export const isCached = async (data: any) => {
8-
const hash = sha256(JSON.stringify(data)).toString()
9-
10-
return readFile(join(__dirname, `../cache/${hash}.svg`))
11-
.then(() => true)
12-
.catch(() => false)
13-
}
14-
15-
export const cache = async (data: any, svg: string) => {
16-
const hash = sha256(JSON.stringify(data)).toString()
17-
18-
logger.info(`Caching ${hash}`)
19-
20-
await appendFile(join(__dirname, `../cache/${hash}.svg`), svg)
21-
}
22-
23-
export const getCache = async (data: any) => {
24-
const hash = sha256(JSON.stringify(data)).toString()
25-
26-
logger.info(`Using cache ${hash}`)
27-
28-
return await readFile(join(__dirname, `../cache/${hash}.svg`), 'utf-8')
29-
}

src/index.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { cache, getCache, isCached } from './cache'
21
import { discordCard } from './cards/discord'
32
import { spotifyCard } from './cards/spotify'
3+
import CacheManager from './structures/cache'
44
import DataManager from './structures/data'
55
import fastify from 'fastify'
66
import { readFile } from 'fs/promises'
@@ -20,7 +20,8 @@ const app = fastify({
2020

2121
export const logger = app.log
2222

23-
const manager = new DataManager(logger)
23+
const dataManager = new DataManager(logger)
24+
const cacheManager = new CacheManager(logger)
2425

2526
app.get('/', async (_, reply) => {
2627
reply.redirect('https://github.com/star0202/discord-profile')
@@ -41,23 +42,17 @@ app.get('/spotify/:id', async (request, reply) => {
4142

4243
let data
4344
if (redirect === 'true') {
44-
data = await manager.spotify(id, true)
45+
data = await dataManager.spotify(id, true)
4546

4647
if (data === undefined) return reply.code(400).send('User Not Found')
4748

4849
reply.redirect(data ?? 'https://github.com/star0202/discord-profile')
4950
} else {
50-
data = await manager.spotify(id, false)
51+
data = await dataManager.spotify(id, false)
5152

5253
if (data === undefined) return reply.code(400).send('User Not Found')
5354

54-
let card
55-
if (await isCached(data)) {
56-
card = await getCache(data)
57-
} else {
58-
card = await spotifyCard(data)
59-
await cache(data, card)
60-
}
55+
const card = await cacheManager.generateCachedCard(data, spotifyCard)
6156

6257
reply
6358
.code(200)
@@ -71,17 +66,11 @@ app.get('/discord/:id', async (request, reply) => {
7166
if (!request.params) return reply.code(400).send('No User ID Provided')
7267

7368
const { id } = request.params as { id: string }
74-
const data = await manager.discord(id)
69+
const data = await dataManager.discord(id)
7570

7671
if (!data) return reply.code(400).send('User Not Found')
7772

78-
let card
79-
if (await isCached(data)) {
80-
card = await getCache(data)
81-
} else {
82-
card = await discordCard(data)
83-
await cache(data, card)
84-
}
73+
const card = await cacheManager.generateCachedCard(data, discordCard)
8574

8675
reply
8776
.code(200)

src/structures/cache.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import sha256 from 'crypto-js/sha256'
3+
import type { FastifyBaseLogger } from 'fastify'
4+
import { appendFile, readFile } from 'fs/promises'
5+
import { join } from 'path'
6+
7+
export default class CacheManager {
8+
private logger: FastifyBaseLogger
9+
10+
constructor(logger: FastifyBaseLogger) {
11+
this.logger = logger.child({ name: 'Cache' })
12+
}
13+
14+
async isCached(data: any) {
15+
const hash = sha256(JSON.stringify(data)).toString()
16+
17+
return readFile(join(__dirname, `../../cache/${hash}.svg`))
18+
.then(() => true)
19+
.catch(() => false)
20+
}
21+
22+
async cache(data: any, svg: string) {
23+
const hash = sha256(JSON.stringify(data)).toString()
24+
25+
this.logger.info(`Caching ${hash}`)
26+
27+
await appendFile(join(__dirname, `../../cache/${hash}.svg`), svg)
28+
}
29+
30+
async getCache(data: any) {
31+
const hash = sha256(JSON.stringify(data)).toString()
32+
33+
this.logger.info(`Using cache ${hash}`)
34+
35+
return await readFile(join(__dirname, `../../cache/${hash}.svg`), 'utf-8')
36+
}
37+
38+
async generateCachedCard(
39+
data: any,
40+
generate: (data: any) => Promise<string>
41+
) {
42+
if (await this.isCached(data)) return await this.getCache(data)
43+
44+
const svg = await generate(data)
45+
await this.cache(data, svg)
46+
47+
return svg
48+
}
49+
}

src/structures/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class DataManager {
1212
this.bot = new Bot(logger)
1313
this.lanyard = new LanyardRequest(logger)
1414

15-
this.logger = logger.child({ name: 'DataManager' })
15+
this.logger = logger.child({ name: 'Data' })
1616

1717
this.bot.start()
1818
}

0 commit comments

Comments
 (0)