-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: channel data with cache #318
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,9 @@ import { TZDate } from '@date-fns/tz' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { repository } from '@next-orders/database' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { getDayIndexByDay, getDayOfWeekByIndex } from './../../../shared/utils/date' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export default defineEventHandler(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let cacheUpdatedAt: string | null = null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export default defineCachedEventHandler(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { channelId } = useRuntimeConfig() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!channelId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -45,4 +47,26 @@ export default defineEventHandler(async () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw errorResolver(error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
shouldInvalidateCache, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Determines if the cache should be invalidated based on channel updatedAt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* True if cache should be invalidated, false otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
async function shouldInvalidateCache(): Promise<boolean> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { channelId } = useRuntimeConfig() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const channel = await repository.channel.find(channelId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!channel) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (cacheUpdatedAt !== channel.updatedAt) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cacheUpdatedAt = channel.updatedAt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+58
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve cache invalidation robustness and performance. The current implementation has several areas for improvement:
Consider this enhanced implementation: async function shouldInvalidateCache(): Promise<boolean> {
const { channelId } = useRuntimeConfig()
+ if (!channelId) {
+ return true
+ }
- const channel = await repository.channel.find(channelId)
+ try {
+ const channel = await repository.channel.find(channelId)
+ if (!channel) {
+ return true
+ }
- if (!channel) {
- return true
- }
+ if (cacheUpdatedAt !== channel.updatedAt) {
+ console.log(`Cache invalidated for channel ${channelId}. Previous: ${cacheUpdatedAt}, New: ${channel.updatedAt}`)
+ cacheUpdatedAt = channel.updatedAt
+ return true
+ }
- if (cacheUpdatedAt !== channel.updatedAt) {
- cacheUpdatedAt = channel.updatedAt
- return true
+ return false
+ } catch (error) {
+ console.error('Error checking cache validity:', error)
+ return true
}
-
- return false
} Consider implementing a more sophisticated caching strategy:
📝 Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using a more robust caching solution.
The current implementation using a global variable for cache state has several potential issues:
Consider these alternatives: