-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
important commit, cache.clear() WORKS, detect database change in anot…
…her process and clear cache on page request, set Date to string in DbMonth type
- Loading branch information
Showing
16 changed files
with
149 additions
and
132 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
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
Binary file not shown.
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
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,63 @@ | ||
import { cacheDatabaseWrapper, getCacheDatabase } from '@/libs/keyv'; | ||
import logger from '@/libs/winston'; | ||
import { CACHE_KEYS_DATABASE } from '@/constants/cache'; | ||
import { getAllMonths } from './month'; | ||
|
||
import { DbMonth } from '@/types/database'; | ||
|
||
const { getUpdatedAtCacheKey } = CACHE_KEYS_DATABASE; | ||
|
||
export const getUpdatedAt = (): string[] => getAllMonths().map((month) => month.updatedAt); | ||
|
||
export const getUpdatedAtCached = () => cacheDatabaseWrapper(getUpdatedAtCacheKey, getUpdatedAt); | ||
|
||
export const findUpdatedMonth = ( | ||
allMonths: DbMonth[], | ||
updatedAtArray: string[] | ||
): DbMonth | undefined => { | ||
const updatedAtSet = new Set(updatedAtArray); | ||
|
||
const dbMonth = allMonths.find((month) => !updatedAtSet.has(month.updatedAt)); | ||
if (dbMonth) return dbMonth; | ||
|
||
const allMonthsUpdatedAtSet = new Set(allMonths.map((month) => month.updatedAt)); | ||
const missingUpdatedAt = updatedAtArray.find( | ||
(updatedAt) => !allMonthsUpdatedAtSet.has(updatedAt) | ||
); | ||
if (missingUpdatedAt) { | ||
return { | ||
name: 'missing-in-db', | ||
threadId: 'missing-in-db', | ||
createdAtOriginal: new Date().toISOString(), | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date(missingUpdatedAt).toISOString(), | ||
}; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export const getUpdatedMonth = async (): Promise<DbMonth | undefined> => { | ||
const allMonths = getAllMonths(); | ||
const updatedAtArrayCached = await getUpdatedAtCached(); | ||
|
||
const updatedMonth = findUpdatedMonth(allMonths, updatedAtArrayCached); | ||
|
||
return updatedMonth; | ||
}; | ||
|
||
/** This must run on every request, to detect change. */ | ||
|
||
export const clearCacheIfDatabaseUpdated = async (): Promise<DbMonth | undefined> => { | ||
const updatedMonth = await getUpdatedMonth(); | ||
|
||
if (updatedMonth) { | ||
logger.info('Database changed, clearing cache, updatedMonth:', updatedMonth); | ||
await getCacheDatabase().clear(); | ||
|
||
// populate cache again | ||
await getUpdatedAtCached(); | ||
} | ||
|
||
return updatedMonth; | ||
}; |
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
Oops, something went wrong.