@@ -195,23 +195,23 @@ export async function queryMembersAdvanced(
195195 // Initialize cache
196196 const cache = new MemberQueryCache ( redis )
197197
198- // Build cache key
198+ // Build cache key — countOnly is excluded so count and full-result caches share the same key.
199+ // Normalize empty search to null so "" and null produce the same key (buildSearchCTE treats them identically).
199200 const cacheKey = cache . buildCacheKey ( {
200- countOnly,
201201 fields,
202202 filter,
203203 include,
204204 includeAllAttributes,
205205 limit,
206206 offset,
207207 orderBy,
208- search,
208+ search : search || null ,
209209 segmentId,
210210 } )
211211
212212 // Try to get from cache first
213213 const cachedResult = countOnly ? null : await cache . get ( cacheKey )
214- const cachedCount = countOnly ? null : await cache . getCount ( cacheKey )
214+ const cachedCount = countOnly ? await cache . getCount ( cacheKey ) : null
215215
216216 if ( cachedResult ) {
217217 refreshCacheInBackground ( bgQx , redis , cacheKey , {
@@ -343,7 +343,9 @@ export async function executeQuery(
343343 withAggregates,
344344 searchConfig,
345345 filterString,
346- includeMemberOrgs : include . memberOrganizations ,
346+ // Count never needs org data in SELECT — filterHasMo inside buildCountQuery already
347+ // handles the case where the filter itself references mo.* columns.
348+ includeMemberOrgs : false ,
347349 } )
348350
349351 if ( countOnly ) {
@@ -537,7 +539,10 @@ export async function executeQuery(
537539
538540 const result = { rows, count, limit, offset }
539541
540- await cache . set ( cacheKey , result , 21600 ) // 6 hours TTL
542+ await Promise . all ( [
543+ cache . set ( cacheKey , result , 21600 ) , // 6 hours TTL
544+ cache . setCount ( cacheKey , count , 21600 ) , // also warm count cache so countOnly=true hits next time
545+ ] )
541546
542547 return result
543548}
0 commit comments