hash args in the cache key

This commit is contained in:
Hongbo Wu
2024-08-14 09:26:34 +08:00
parent c2ec95845e
commit d00a76ca3f

View File

@ -35,6 +35,7 @@ import { logger } from '../utils/logger'
import { parseSearchQuery } from '../utils/search'
import { HighlightEvent } from './highlights'
import { addLabelsToLibraryItem, LabelEvent } from './labels'
import { stringToHash } from '../utils/helpers'
const columnsToDelete = [
'user',
@ -704,7 +705,9 @@ export const createSearchQueryBuilder = (
}
export const countLibraryItems = async (args: SearchArgs, userId: string) => {
const cacheKey = `countLibraryItems:${userId}:${JSON.stringify(args)}`
// hash the arguments to create a unique cache key
const argsHash = stringToHash(JSON.stringify(args))
const cacheKey = `countLibraryItems:${userId}:${argsHash}`
const cachedCount = await redisDataSource.redisClient?.get(cacheKey)
if (cachedCount) {
return parseInt(cachedCount, 10)