Skip to content

Commit bbfe9ab

Browse files
committed
refactor: refresh in background
1 parent b70af69 commit bbfe9ab

1 file changed

Lines changed: 114 additions & 18 deletions

File tree

  • services/libs/data-access-layer/src/members

services/libs/data-access-layer/src/members/base.ts

Lines changed: 114 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,93 @@ export async function queryMembersAdvanced(
162162
})
163163

164164
// Try to get from cache first
165-
if (!countOnly) {
166-
const cachedResult = await cache.get(cacheKey)
167-
if (cachedResult) {
168-
log.info('Returning cached result for members query')
169-
return cachedResult
170-
}
171-
} else {
172-
const cachedCount = await cache.getCount(cacheKey)
173-
if (cachedCount !== null) {
174-
log.info('Returning cached count for members query')
175-
return {
176-
rows: [],
177-
count: cachedCount,
178-
limit,
179-
offset,
180-
}
165+
const cachedResult = countOnly ? null : await cache.get(cacheKey)
166+
const cachedCount = countOnly ? await cache.getCount(cacheKey) : null
167+
168+
if (cachedResult) {
169+
refreshCacheInBackground(qx, redis, cacheKey, {
170+
filter,
171+
search,
172+
limit,
173+
offset,
174+
orderBy,
175+
segmentId,
176+
countOnly: false,
177+
fields,
178+
include,
179+
attributeSettings,
180+
})
181+
182+
log.info(`Members advanced query cache hit: ${cacheKey}`)
183+
return cachedResult
184+
}
185+
186+
if (countOnly && cachedCount !== null) {
187+
refreshCountCacheInBackground(qx, redis, cacheKey, {
188+
filter,
189+
search,
190+
segmentId,
191+
include,
192+
attributeSettings,
193+
})
194+
195+
log.info(`Members advanced count query cache hit: ${cacheKey}`)
196+
return {
197+
rows: [],
198+
count: cachedCount,
199+
limit,
200+
offset,
181201
}
182202
}
183203

204+
log.info(`Executing members advanced query: ${cacheKey}`)
205+
206+
return await executeQuery(qx, redis, cacheKey, {
207+
filter,
208+
search,
209+
limit,
210+
offset,
211+
orderBy,
212+
segmentId,
213+
countOnly,
214+
fields,
215+
include,
216+
attributeSettings,
217+
})
218+
}
219+
220+
export async function executeQuery(
221+
qx: QueryExecutor,
222+
redis: RedisClient,
223+
cacheKey: string,
224+
{
225+
filter = {},
226+
search = null,
227+
limit = 20,
228+
offset = 0,
229+
orderBy = 'activityCount_DESC',
230+
segmentId = undefined,
231+
countOnly = false,
232+
fields = [...QUERY_FILTER_COLUMN_MAP.keys()],
233+
include = {
234+
identities: true,
235+
segments: false,
236+
lfxMemberships: false,
237+
memberOrganizations: false,
238+
onlySubProjects: false,
239+
maintainers: true,
240+
} as {
241+
identities?: boolean
242+
segments?: boolean
243+
lfxMemberships?: boolean
244+
memberOrganizations?: boolean
245+
onlySubProjects?: boolean
246+
maintainers?: boolean
247+
},
248+
attributeSettings = [] as IDbMemberAttributeSetting[],
249+
},
250+
): Promise<PageData<IDbMemberData>> {
251+
const cache = new MemberQueryCache(redis)
184252
const withAggregates = !!segmentId
185253
const searchConfig = buildSearchCTE(search)
186254

@@ -233,7 +301,7 @@ export async function queryMembersAdvanced(
233301
const count = parseInt(result.count, 10)
234302

235303
// Cache the count
236-
await cache.setCount(cacheKey, count, 300) // 5 minutes TTL
304+
await cache.setCount(cacheKey, count, 1800) // 30 minutes TTL
237305

238306
return {
239307
rows: [],
@@ -388,11 +456,39 @@ export async function queryMembersAdvanced(
388456
const result = { rows, count, limit, offset }
389457

390458
// Cache the result
391-
await cache.set(cacheKey, result, 300) // 5 minutes TTL
459+
await cache.set(cacheKey, result, 1800) // 30 minutes TTL
392460

393461
return result
394462
}
395463

464+
async function refreshCacheInBackground(
465+
qx: QueryExecutor,
466+
redis: RedisClient,
467+
cacheKey: string,
468+
params: any,
469+
): Promise<void> {
470+
try {
471+
log.info(`Refreshing members advanced query cache in background: ${cacheKey}`)
472+
await executeQuery(qx, redis, cacheKey, params)
473+
} catch (error) {
474+
log.warn('Background cache refresh failed:', error)
475+
}
476+
}
477+
478+
async function refreshCountCacheInBackground(
479+
qx: QueryExecutor,
480+
redis: RedisClient,
481+
cacheKey: string,
482+
params: any,
483+
): Promise<void> {
484+
try {
485+
log.info(`Refreshing members advanced count cache in background: ${cacheKey}`)
486+
await executeQuery(qx, redis, cacheKey, { ...params, countOnly: true })
487+
} catch (error) {
488+
log.warn('Background count cache refresh failed:', error)
489+
}
490+
}
491+
396492
export async function queryMembers<T extends MemberField>(
397493
qx: QueryExecutor,
398494
opts: QueryOptions<T>,

0 commit comments

Comments
 (0)