From 62ea44035708028891556e8bed4055056a1809bb Mon Sep 17 00:00:00 2001 From: Florindo Date: Wed, 27 Apr 2022 13:30:14 +0200 Subject: [PATCH 1/5] add cache --- interceptors/DataApiInterceptors.cfc | 17 +++++++++++++++++ services/DataApiService.cfc | 24 ++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/interceptors/DataApiInterceptors.cfc b/interceptors/DataApiInterceptors.cfc index 3878f3c..cc69c51 100755 --- a/interceptors/DataApiInterceptors.cfc +++ b/interceptors/DataApiInterceptors.cfc @@ -165,4 +165,21 @@ component extends="coldbox.system.Interceptor" { dataApiQueueService.queueInsert( argumentCollection=interceptData ); } + + + + 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 ); + + var cachedResult = queryCache.get( interceptData.cacheKey ); + //writeDump(interceptData);abort; + if ( !IsNull( local.cachedResult ) ) { + event.setValue( name="getFromCache", value=true, private=true ); + } + } + } \ No newline at end of file diff --git a/services/DataApiService.cfc b/services/DataApiService.cfc index 6d45e28..b065040 100644 --- a/services/DataApiService.cfc +++ b/services/DataApiService.cfc @@ -279,12 +279,28 @@ 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 cachedProcessed= queryCache.get( "dataApi_#cacheKey#" ); + + var getFromCache = false; + if ( !IsNull( local.cachedProcessed ) ) { + getFromCache = true + } + + getFromCache = getFromCache && 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; } From 74bf79b5c08c657b5d65a966123b022f659d0872 Mon Sep 17 00:00:00 2001 From: flolopdel Date: Fri, 29 Apr 2022 17:21:19 +0200 Subject: [PATCH 2/5] Update DataApiInterceptors.cfc --- interceptors/DataApiInterceptors.cfc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interceptors/DataApiInterceptors.cfc b/interceptors/DataApiInterceptors.cfc index cc69c51..9ac4569 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; @@ -176,10 +177,9 @@ component extends="coldbox.system.Interceptor" { event.setValue( name="getFromCache", value=false, private=true ); var cachedResult = queryCache.get( interceptData.cacheKey ); - //writeDump(interceptData);abort; if ( !IsNull( local.cachedResult ) ) { - event.setValue( name="getFromCache", value=true, private=true ); + event.setValue( name="getFromCache", value=true, private=true ); } } -} \ No newline at end of file +} From ccbdcb63de71859cf7480c304ff1da20f6779ac6 Mon Sep 17 00:00:00 2001 From: flolopdel Date: Tue, 3 May 2022 13:48:21 +0200 Subject: [PATCH 3/5] check for the object which does not use cache --- interceptors/DataApiInterceptors.cfc | 1 + services/DataApiService.cfc | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/interceptors/DataApiInterceptors.cfc b/interceptors/DataApiInterceptors.cfc index 9ac4569..493cea9 100755 --- a/interceptors/DataApiInterceptors.cfc +++ b/interceptors/DataApiInterceptors.cfc @@ -175,6 +175,7 @@ component extends="coldbox.system.Interceptor" { 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 ) ) { diff --git a/services/DataApiService.cfc b/services/DataApiService.cfc index b065040..7c8752e 100644 --- a/services/DataApiService.cfc +++ b/services/DataApiService.cfc @@ -282,14 +282,14 @@ component { 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 getFromCache = false; - if ( !IsNull( local.cachedProcessed ) ) { - getFromCache = true + if ( useCache && !IsNull( local.cachedProcessed ) ) { + useCache = true } - getFromCache = getFromCache && event.getValue( name="getFromCache", default =false, private=true ); + var getFromCache = useCache && event.getValue( name="getFromCache", default =false, private=true ); if(getFromCache){ var processed = local.cachedProcessed; From ca095687670c8336db447422cd5c19dd77886910 Mon Sep 17 00:00:00 2001 From: flolopdel Date: Tue, 3 May 2022 13:55:51 +0200 Subject: [PATCH 4/5] double check in the cache --- services/DataApiService.cfc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/services/DataApiService.cfc b/services/DataApiService.cfc index 7c8752e..df0b3ea 100644 --- a/services/DataApiService.cfc +++ b/services/DataApiService.cfc @@ -285,11 +285,12 @@ component { var useCache = event.getValue( name="#objectName#_useCache", default =false, private=true ); var cachedProcessed= queryCache.get( "dataApi_#cacheKey#" ); - if ( useCache && !IsNull( local.cachedProcessed ) ) { - useCache = true + var existsCache = false; + if ( !IsNull( local.cachedProcessed ) ) { + existsCache = true } - var getFromCache = useCache && event.getValue( name="getFromCache", default =false, private=true ); + var getFromCache = useCache && existsCache && event.getValue( name="getFromCache", default =false, private=true ); if(getFromCache){ var processed = local.cachedProcessed; From 5d9b4726be78aa0693fbf9f5be6ad0d11e39cda7 Mon Sep 17 00:00:00 2001 From: flolopdel Date: Tue, 3 May 2022 13:56:02 +0200 Subject: [PATCH 5/5] ignore vscode --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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