Merge pull request #4251 from omnivore-app/perf/search

cache total count of library items in 60 seconds
This commit is contained in:
Hongbo Wu
2024-08-13 12:21:57 +08:00
committed by GitHub
2 changed files with 19 additions and 12 deletions

View File

@ -704,13 +704,22 @@ export const createSearchQueryBuilder = (
}
export const countLibraryItems = async (args: SearchArgs, userId: string) => {
return authTrx(
const cacheKey = `countLibraryItems:${userId}:${JSON.stringify(args)}`
const cachedCount = await redisDataSource.redisClient?.get(cacheKey)
if (cachedCount) {
return parseInt(cachedCount, 10)
}
const count = await authTrx(
async (tx) => createSearchQueryBuilder(args, userId, tx).getCount(),
{
uid: userId,
replicationMode: 'replica',
}
)
await redisDataSource.redisClient?.set(cacheKey, count, 'EX', 60)
return count
}
export const searchLibraryItems = async (

View File

@ -27,6 +27,7 @@ import { getRepository } from '../../src/repository'
import { createGroup, deleteGroup } from '../../src/services/groups'
import { createLabel, deleteLabels } from '../../src/services/labels'
import {
countLibraryItems,
createLibraryItems,
createOrUpdateLibraryItem,
CreateOrUpdateLibraryItemArgs,
@ -2451,19 +2452,16 @@ describe('Article API', () => {
})
it('empties the trash', async () => {
let response = await graphqlRequest(
searchQuery('in:trash'),
authToken
).expect(200)
expect(response.body.data.search.pageInfo.totalCount).to.eql(5)
await graphqlRequest(emptyTrashQuery(), authToken).expect(200)
response = await graphqlRequest(
searchQuery('in:trash'),
authToken
).expect(200)
expect(response.body.data.search.pageInfo.totalCount).to.eql(0)
const count = await countLibraryItems(
{
query: 'in:trash',
includeDeleted: true,
},
user.id
)
expect(count).to.eql(0)
})
})
})