88 QueryColumnsResult ,
99 QueryKey } from '@cubejs-backend/base-driver' ;
1010
11- import { QueryCache , QueryBody , TempTable , PreAggTableToTempTable } from './QueryCache' ;
11+ import { QueryCache , QueryBody , TempTable , PreAggTableToTempTable , QueryWithParams , CacheKey } from './QueryCache' ;
1212import { PreAggregations , PreAggregationDescription , getLastUpdatedAtTimestamp } from './PreAggregations' ;
1313import { DriverFactory , DriverFactoryByDataSource } from './DriverFactory' ;
1414import { QueryStream } from './QueryStream' ;
@@ -439,6 +439,14 @@ export class QueryOrchestrator {
439439 return this . preAggregations . updateRefreshEndReached ( ) ;
440440 }
441441
442+ private createMetadataQuery ( operation : string , params : Record < string , any > ) : QueryWithParams {
443+ return [
444+ `METADATA:${ operation } ` ,
445+ [ JSON . stringify ( params ) ] ,
446+ { external : false , renewalThreshold : 24 * 60 * 60 }
447+ ] ;
448+ }
449+
442450 private async queryDataSourceMetadata < T > (
443451 operation : MetadataOperationType ,
444452 params : Record < string , any > ,
@@ -452,74 +460,35 @@ export class QueryOrchestrator {
452460 ) : Promise < T > {
453461 const {
454462 requestId,
455- forceRefresh,
456- expiration
463+ forceRefresh = false ,
464+ renewalThreshold = 24 * 60 * 60 ,
465+ expiration = 7 * 24 * 60 * 60 ,
457466 } = options ;
458467
459- // Create a unique cache key for this metadata request
460- const cacheKey = `METADATA:${ operation } :${ dataSource } :${ JSON . stringify ( params ) } ` ;
461- const cacheDriver = this . queryCache . getCacheDriver ( ) ;
462-
463- // Check cache first (unless forceRefresh is true)
464- if ( ! forceRefresh ) {
465- try {
466- const cachedResult = await cacheDriver . get ( cacheKey ) ;
467- if ( cachedResult && cachedResult . result ) {
468- this . logger ( 'Found cached metadata result' , { cacheKey, operation, dataSource } ) ;
469- return cachedResult . result ;
470- }
471- } catch ( e ) {
472- this . logger ( 'Error reading from cache' , { cacheKey, error : e instanceof Error ? e . message : String ( e ) } ) ;
473- }
474- }
475-
476- // If not in cache or forceRefresh, execute through queue
477- const metadataRequest = {
478- operation,
479- params,
480- external : false ,
481- type : 'metadata'
482- } ;
483-
484- // Create unique queue key for forceRefresh to avoid conflicts
485- const queueKey = forceRefresh
486- ? `${ cacheKey } :${ new Date ( ) . getTime ( ) } `
487- : cacheKey ;
468+ const metadataQuery = this . createMetadataQuery ( operation , params ) ;
469+ const cacheKey : CacheKey = [ `METADATA:${ operation } ` , metadataQuery , dataSource ] ;
488470
489- // Get queue for the given datasource
490- const queue = await this . queryCache . getQueue ( dataSource ) ;
491-
492- // Execute metadata request through the queue
493- const result = await queue . executeInQueue (
494- 'metadata' ,
495- queueKey ,
471+ const renewalKey = forceRefresh ? undefined : [
472+ `METADATA_RENEWAL:${ operation } ` ,
473+ dataSource ,
474+ Math . floor ( Date . now ( ) / ( renewalThreshold * 1000 ) )
475+ ] ;
476+
477+ return this . queryCache . cacheQueryResult (
478+ metadataQuery ,
479+ [ ] ,
480+ cacheKey ,
481+ expiration ,
496482 {
497- ...metadataRequest ,
483+ renewalThreshold,
484+ renewalKey,
485+ forceNoCache : forceRefresh ,
486+ requestId,
498487 dataSource,
499- requestId
500- } ,
501- 10 , // priority
502- {
503- stageQueryKey : queueKey ,
504- requestId
488+ useInMemory : true ,
489+ waitForRenew : true ,
505490 }
506491 ) ;
507-
508- // Store result in cache driver (unless forceRefresh)
509- if ( ! forceRefresh && result ) {
510- try {
511- const cacheValue = {
512- time : new Date ( ) . getTime ( ) ,
513- result
514- } ;
515- await cacheDriver . set ( cacheKey , cacheValue , expiration || ( 24 * 60 * 60 ) ) ; // Default 24 hours
516- this . logger ( 'Stored metadata result in cache' , { cacheKey, operation, dataSource } ) ;
517- } catch ( e ) {
518- this . logger ( 'Error storing in cache' , { cacheKey, error : e instanceof Error ? e . message : String ( e ) } ) ;
519- }
520- }
521-
522- return result ;
523492 }
524493
525494 /**
0 commit comments