diff --git a/src/controllers/forward.ts b/src/controllers/forward.ts index 7d6568f3..ca014ea4 100644 --- a/src/controllers/forward.ts +++ b/src/controllers/forward.ts @@ -50,6 +50,9 @@ export const postProcessForward = async (payload: ForwardMeta, res?: NextApiResp return cacheMissed ? badRequest(res) : null; } + // write back to cache + shortenCacheService.postShortenHash(history); + await prisma.urlForwardMeta.upsert({ where: { userAgent_ip_urlShortenerHistoryId: { @@ -72,8 +75,6 @@ export const postProcessForward = async (payload: ForwardMeta, res?: NextApiResp }); if (cacheMissed) { - // write back to cache - await shortenCacheService.postShortenHash(history); return res.status(HttpStatusCode.OK).json({ history }); } }; diff --git a/src/controllers/shorten/update.ts b/src/controllers/shorten/update.ts index 06fb511e..322c6f36 100644 --- a/src/controllers/shorten/update.ts +++ b/src/controllers/shorten/update.ts @@ -54,7 +54,7 @@ export const handler = api( }, }); // update cache - shortenCacheService.updateShortenHash(hash); + shortenCacheService.expireShortenHash(hash); // remove used tag if (ogImgPublicId) { cloudinaryInstance.uploader.remove_tag('unused', ogImgPublicId).then().catch(); diff --git a/src/services/cacheServices/shorten.service.ts b/src/services/cacheServices/shorten.service.ts index 9cab4bcb..d687b811 100644 --- a/src/services/cacheServices/shorten.service.ts +++ b/src/services/cacheServices/shorten.service.ts @@ -44,7 +44,7 @@ export class ShortenCache { await Promise.all([redis.hset(hashKey, data), redis.expire(hashKey, LIMIT_SHORTENED_SECOND)]); } - updateShortenHash(hash: string) { + expireShortenHash(hash: string) { const hashKey = getRedisKey(REDIS_KEY.MAP_SHORTEN_BY_HASH, hash); redis.expire(hashKey, -1).then().catch(); }