diff --git a/.gitignore b/.gitignore index 672f054..59126ff 100755 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ npm-debug.log /tests/server*.json /modules +.vscode/settings.json diff --git a/interceptors/DataApiInterceptors.cfc b/interceptors/DataApiInterceptors.cfc index 3878f3c..493cea9 100755 --- a/interceptors/DataApiInterceptors.cfc +++ b/interceptors/DataApiInterceptors.cfc @@ -4,6 +4,7 @@ component extends="coldbox.system.Interceptor" { property name="dataApiQueueService" inject="delayedInjector:dataApiQueueService"; property name="dataApiConfigurationService" inject="delayedInjector:dataApiConfigurationService"; property name="interceptorService" inject="coldbox:InterceptorService"; + property name="queryCache" inject="cachebox:DefaultQueryCache"; variables._applicationLoaded = false; @@ -165,4 +166,21 @@ component extends="coldbox.system.Interceptor" { dataApiQueueService.queueInsert( argumentCollection=interceptData ); } -} \ No newline at end of file + + + + public void function onCreateSelectDataCacheKey( event, interceptData ) { + if ( !_applicationLoaded ) return; + + var objectName = interceptData.objectName ?: ""; + event.setValue( name="cacheKey", value=interceptData.cacheKey ?: "", private=true ); + event.setValue( name="getFromCache", value=false, private=true ); + event.setValue( name="#objectName#_useCache", value=true, private=true ); + + var cachedResult = queryCache.get( interceptData.cacheKey ); + if ( !IsNull( local.cachedResult ) ) { + event.setValue( name="getFromCache", value=true, private=true ); + } + } + +} diff --git a/services/DataApiService.cfc b/services/DataApiService.cfc index 6d45e28..df0b3ea 100644 --- a/services/DataApiService.cfc +++ b/services/DataApiService.cfc @@ -279,12 +279,29 @@ component { } var records = dao.selectData( argumentCollection=args ); - var processed = []; - for( var record in records ) { - processed.append( _processFields( record, fieldSettings ) ); + var event = $getRequestContext(); + var cacheKey = event.getValue( name="cacheKey", default ="", private=true ); + var useCache = event.getValue( name="#objectName#_useCache", default =false, private=true ); + var cachedProcessed= queryCache.get( "dataApi_#cacheKey#" ); + + var existsCache = false; + if ( !IsNull( local.cachedProcessed ) ) { + existsCache = true + } + + var getFromCache = useCache && existsCache && event.getValue( name="getFromCache", default =false, private=true ); + + if(getFromCache){ + var processed = local.cachedProcessed; + }else{ + var processed = []; + for( var record in records ) { + processed.append( _processFields( record, fieldSettings ) ); + } + $announceInterception( "postDataApiSelectData#namespace#", { selectDataArgs=args, entity=arguments.entity, data=processed } ); + queryCache.set( "dataApi_#cacheKey#", processed); } - $announceInterception( "postDataApiSelectData#namespace#", { selectDataArgs=args, entity=arguments.entity, data=processed } ); return processed; }