-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from sgcarstrends/58-update-months-endpoint
Refactor months endpoint
- Loading branch information
Showing
5 changed files
with
45 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import db from "../config/db"; | ||
import redis from "../config/redis"; | ||
|
||
const CACHE_TTL = 60 * 60 * 24; // 1 day in seconds | ||
|
||
export const getUniqueMonths = async (collection, key = "month") => { | ||
const CACHE_KEY = `${collection}:months`; | ||
|
||
let months: string[] = await redis.smembers(CACHE_KEY); | ||
if (months.length === 0) { | ||
months = await db.collection(collection).distinct(key); | ||
await redis.sadd(CACHE_KEY, ...months); | ||
await redis.expire(CACHE_KEY, CACHE_TTL); | ||
} | ||
|
||
return months.sort((a, b) => b.localeCompare(a)); | ||
}; |
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
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