@@ -26,6 +26,8 @@ import {
2626 UnloadOptions ,
2727} from '@cubejs-backend/base-driver' ;
2828import { CubeStoreDriver } from '@cubejs-backend/cubestore-driver' ;
29+ import LRUCache from 'lru-cache' ;
30+
2931import { PreAggTableToTempTable , Query , QueryBody , QueryCache , QueryTuple , QueryWithParams } from './QueryCache' ;
3032import { ContinueWaitError } from './ContinueWaitError' ;
3133import { DriverFactory , DriverFactoryByDataSource } from './DriverFactory' ;
@@ -1968,6 +1970,8 @@ export class PreAggregations {
19681970
19691971 private readonly getQueueEventsBus : any ;
19701972
1973+ private readonly touchCache : LRUCache < string , true > ;
1974+
19711975 public constructor (
19721976 private readonly redisPrefix : string ,
19731977 private readonly driverFactory : DriverFactoryByDataSource ,
@@ -1984,14 +1988,20 @@ export class PreAggregations {
19841988 this . usedTablePersistTime = options . usedTablePersistTime || getEnv ( 'dbQueryTimeout' ) ;
19851989 this . externalRefresh = options . externalRefresh ;
19861990 this . getQueueEventsBus = options . getQueueEventsBus ;
1991+ this . touchCache = new LRUCache ( {
1992+ max : getEnv ( 'touchPreAggregationCacheMaxCount' ) ,
1993+ maxAge : getEnv ( 'touchPreAggregationCacheMaxAge' ) * 1000 ,
1994+ stale : false ,
1995+ updateAgeOnGet : false
1996+ } ) ;
19871997 }
19881998
1989- protected tablesUsedRedisKey ( tableName ) {
1999+ protected tablesUsedRedisKey ( tableName : string ) : string {
19902000 // TODO add dataSource?
19912001 return this . queryCache . getKey ( 'SQL_PRE_AGGREGATIONS_TABLES_USED' , tableName ) ;
19922002 }
19932003
1994- protected tablesTouchRedisKey ( tableName ) {
2004+ protected tablesTouchRedisKey ( tableName : string ) : string {
19952005 // TODO add dataSource?
19962006 return this . queryCache . getKey ( 'SQL_PRE_AGGREGATIONS_TABLES_TOUCH' , tableName ) ;
19972007 }
@@ -2001,17 +2011,37 @@ export class PreAggregations {
20012011 return this . queryCache . getKey ( 'SQL_PRE_AGGREGATIONS_REFRESH_END_REACHED' , '' ) ;
20022012 }
20032013
2004- public async addTableUsed ( tableName ) {
2005- return this . queryCache . getCacheDriver ( ) . set ( this . tablesUsedRedisKey ( tableName ) , true , this . usedTablePersistTime ) ;
2014+ public async addTableUsed ( tableName : string ) : Promise < void > {
2015+ await this . queryCache . getCacheDriver ( ) . set (
2016+ this . tablesUsedRedisKey ( tableName ) ,
2017+ true ,
2018+ this . usedTablePersistTime
2019+ ) ;
20062020 }
20072021
20082022 public async tablesUsed ( ) {
20092023 return ( await this . queryCache . getCacheDriver ( ) . keysStartingWith ( this . tablesUsedRedisKey ( '' ) ) )
20102024 . map ( k => k . replace ( this . tablesUsedRedisKey ( '' ) , '' ) ) ;
20112025 }
20122026
2013- public async updateLastTouch ( tableName ) {
2014- return this . queryCache . getCacheDriver ( ) . set ( this . tablesTouchRedisKey ( tableName ) , new Date ( ) . getTime ( ) , this . touchTablePersistTime ) ;
2027+ public async updateLastTouch ( tableName : string ) : Promise < void > {
2028+ if ( this . touchCache . has ( tableName ) ) {
2029+ return ;
2030+ }
2031+
2032+ try {
2033+ this . touchCache . set ( tableName , true ) ;
2034+
2035+ await this . queryCache . getCacheDriver ( ) . set (
2036+ this . tablesTouchRedisKey ( tableName ) ,
2037+ new Date ( ) . getTime ( ) ,
2038+ this . touchTablePersistTime
2039+ ) ;
2040+ } catch ( e : unknown ) {
2041+ this . touchCache . del ( tableName ) ;
2042+
2043+ throw e ;
2044+ }
20152045 }
20162046
20172047 public async tablesTouched ( ) {
0 commit comments