Skip to content

Commit a9b684d

Browse files
Implement rudimentary caching in KVC
1 parent 6a21b46 commit a9b684d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

ng-appserver/src/main/java/ng/appserver/NGApplication.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import ng.appserver.templating.NGElementManager;
3737
import ng.appserver.templating.NGElementManager.ElementProvider;
3838
import ng.appserver.wointegration.NGDefaultLifeBeatThread;
39+
import ng.kvc.NGKeyValueCoding;
3940
import ng.plugins.Elements;
4041
import ng.plugins.NGCorePlugin;
4142
import ng.plugins.NGDevelopmentPlugin;
@@ -162,6 +163,9 @@ public static <E extends NGApplication> E runAndReturn( final String[] args, fin
162163
// If we're in development mode, activate the development plugin for some bonus development features
163164
if( isDevelopmentMode ) {
164165
application.plugins.add( new NGDevelopmentPlugin() );
166+
167+
// FIXME: Most definitely not the way we're going to use to decide if KVC caching is enabled. Under development // Hugi 2025-04-21
168+
NGKeyValueCoding.DefaultImplementation.setCachingEnabled( false );
165169
}
166170

167171
// CHECKME: We probably need more extension points for plugin initialization (pre-constructor, post-constructor etc.) // Hugi 2023-07-28
@@ -368,7 +372,8 @@ private NGRequestHandler handlerForURL( String url ) {
368372
* @return true if we're in development mode
369373
*/
370374
public boolean isDevelopmentMode() {
371-
return _deploymentMode == StandardDeploymentMode.Development;
375+
return false;
376+
// return _deploymentMode == StandardDeploymentMode.Development;
372377
}
373378

374379
/**

ng-core/src/main/java/ng/kvc/NGKeyValueCoding.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.lang.reflect.Modifier;
88
import java.math.BigDecimal;
99
import java.util.List;
10+
import java.util.Map;
1011
import java.util.Objects;
12+
import java.util.concurrent.ConcurrentHashMap;
1113

1214
import ng.NGRuntimeException;
1315

@@ -114,9 +116,45 @@ public static void takeValueForKey( final Object object, final Object value, fin
114116
}
115117

116118
/**
117-
* @return A KVC binding for the given class and key.
119+
* FIXME: Most definitely not the way we're going to use to set if KVC caching is enabled // Hugi 2025-04-21
120+
*/
121+
public static void setCachingEnabled( boolean value ) {
122+
_cachingEnabled = value;
123+
}
124+
125+
/**
126+
* FIXME: Most definitely not the way we're going to use to decide if KVC caching is enabled // Hugi 2025-04-21
118127
*/
128+
public static boolean _cachingEnabled = true;
129+
130+
/**
131+
* FIXME: Currently just a very primitive cache for testing // Hugi 2025-04-21
132+
*/
133+
private static Map<CacheKey, KVCReadBinding> _readBindingCache = new ConcurrentHashMap<>();
134+
135+
private record CacheKey( Class<?> clazz, String key ) {}
136+
119137
private static KVCReadBinding readBindingForKey( final Object object, final String key ) {
138+
139+
if( !_cachingEnabled ) {
140+
return locateBindingForKey( object, key );
141+
}
142+
143+
final CacheKey cacheKey = new CacheKey( object.getClass(), key );
144+
KVCReadBinding entry = _readBindingCache.get( cacheKey );
145+
146+
if( entry == null ) {
147+
entry = locateBindingForKey( object, key );
148+
_readBindingCache.put( cacheKey, entry );
149+
}
150+
151+
return entry;
152+
}
153+
154+
/**
155+
* @return A KVC binding for the given class and key.
156+
*/
157+
private static KVCReadBinding locateBindingForKey( final Object object, final String key ) {
120158
Objects.requireNonNull( object );
121159
Objects.requireNonNull( key );
122160

0 commit comments

Comments
 (0)