dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #40674
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20667: SystemSettingManager, introduced cache for getSystemSetting( Setting ). Improves performance as t...
------------------------------------------------------------
revno: 20667
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-10-13 11:22:30 +0200
message:
SystemSettingManager, introduced cache for getSystemSetting( Setting ). Improves performance as this method is called frequently.
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/Setting.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/setting/SystemSettingManagerTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/Setting.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/Setting.java 2015-10-09 10:59:01 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/Setting.java 2015-10-13 09:22:30 +0000
@@ -116,6 +116,10 @@
private final Class<?> clazz;
+ // -------------------------------------------------------------------------
+ // Constructors
+ // -------------------------------------------------------------------------
+
private Setting( String name )
{
this.name = name;
@@ -136,7 +140,11 @@
this.defaultValue = defaultValue;
this.clazz = clazz;
}
-
+
+ // -------------------------------------------------------------------------
+ // Logic
+ // -------------------------------------------------------------------------
+
public static Optional<Setting> getByName( String name )
{
for ( Setting setting : Setting.values() )
@@ -177,6 +185,15 @@
return value;
}
+ public boolean hasDefaultValue()
+ {
+ return defaultValue != null;
+ }
+
+ // -------------------------------------------------------------------------
+ // Getters
+ // -------------------------------------------------------------------------
+
public String getName()
{
return name;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java 2015-10-09 10:56:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java 2015-10-13 09:22:30 +0000
@@ -124,6 +124,10 @@
void saveSystemSetting( Setting setting, Serializable value );
+ void deleteSystemSetting( String name );
+
+ void deleteSystemSetting( Setting setting );
+
Serializable getSystemSetting( String name );
Serializable getSystemSetting( String name, Serializable defaultValue );
@@ -134,10 +138,6 @@
List<SystemSetting> getAllSystemSettings();
- void deleteSystemSetting( String name );
-
- void deleteSystemSetting( Setting setting );
-
Map<String, Serializable> getSystemSettingsAsMap();
Map<String, Serializable> getSystemSettingsAsMap( Set<String> names );
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java 2015-10-09 10:49:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java 2015-10-13 09:22:30 +0000
@@ -28,10 +28,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import org.apache.commons.lang3.StringUtils;
-import org.hisp.dhis.system.util.ValidationUtils;
-import org.springframework.transaction.annotation.Transactional;
-
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
@@ -40,6 +36,15 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.lang3.StringUtils;
+import org.hisp.dhis.system.util.ValidationUtils;
+import org.springframework.transaction.annotation.Transactional;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
/**
* @author Stian Strandli
@@ -49,6 +54,15 @@
public class DefaultSystemSettingManager
implements SystemSettingManager
{
+ /**
+ * Cache for system settings. Does not accept nulls.
+ */
+ private static Cache<String, Serializable> SETTING_CACHE = CacheBuilder.newBuilder()
+ .expireAfterAccess( 5, TimeUnit.MINUTES )
+ .initialCapacity( 200 )
+ .maximumSize( 400 )
+ .build();
+
// -------------------------------------------------------------------------
// Dependencies
// -------------------------------------------------------------------------
@@ -74,6 +88,8 @@
@Override
public void saveSystemSetting( String name, Serializable value )
{
+ SETTING_CACHE.invalidate( name );
+
SystemSetting setting = systemSettingStore.getByName( name );
if ( setting == null )
@@ -96,9 +112,30 @@
@Override
public void saveSystemSetting( Setting setting, Serializable value )
{
+ SETTING_CACHE.invalidate( setting.getName() );
+
saveSystemSetting( setting.getName(), value );
}
-
+
+ @Override
+ public void deleteSystemSetting( String name )
+ {
+ SystemSetting setting = systemSettingStore.getByName( name );
+
+ if ( setting != null )
+ {
+ SETTING_CACHE.invalidate( name );
+
+ systemSettingStore.delete( setting );
+ }
+ }
+
+ @Override
+ public void deleteSystemSetting( Setting setting )
+ {
+ deleteSystemSetting( setting.getName() );
+ }
+
@Override
public Serializable getSystemSetting( String name )
{
@@ -118,7 +155,21 @@
@Override
public Serializable getSystemSetting( Setting setting )
{
- return getSystemSetting( setting.getName(), setting.getDefaultValue() );
+ if ( setting.hasDefaultValue() )
+ {
+ try
+ {
+ return SETTING_CACHE.get( setting.getName(), () -> getSystemSetting( setting.getName(), setting.getDefaultValue() ) );
+ }
+ catch ( ExecutionException ignored )
+ {
+ return null;
+ }
+ }
+ else
+ {
+ return getSystemSetting( setting.getName(), setting.getDefaultValue() );
+ }
}
@Override
@@ -134,23 +185,6 @@
}
@Override
- public void deleteSystemSetting( String name )
- {
- SystemSetting setting = systemSettingStore.getByName( name );
-
- if ( setting != null )
- {
- systemSettingStore.delete( setting );
- }
- }
-
- @Override
- public void deleteSystemSetting( Setting setting )
- {
- deleteSystemSetting( setting.getName() );
- }
-
- @Override
public Map<String, Serializable> getSystemSettingsAsMap()
{
Map<String, Serializable> settingsMap = new HashMap<>();
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/setting/SystemSettingManagerTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/setting/SystemSettingManagerTest.java 2015-10-06 22:15:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/setting/SystemSettingManagerTest.java 2015-10-13 09:22:30 +0000
@@ -31,6 +31,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
import java.io.Serializable;
import java.util.List;
@@ -78,6 +79,29 @@
}
@Test
+ public void testSaveGetDeleteSetting()
+ {
+ assertNull( systemSettingManager.getSystemSetting( Setting.APPLICATION_INTRO ) );
+ assertEquals( Setting.HELP_PAGE_LINK.getDefaultValue(), systemSettingManager.getSystemSetting( Setting.HELP_PAGE_LINK ) );
+
+ systemSettingManager.saveSystemSetting( Setting.APPLICATION_INTRO, "valueA" );
+ systemSettingManager.saveSystemSetting( Setting.HELP_PAGE_LINK, "valueB" );
+
+ assertEquals( "valueA", systemSettingManager.getSystemSetting( Setting.APPLICATION_INTRO ) );
+ assertEquals( "valueB", systemSettingManager.getSystemSetting( Setting.HELP_PAGE_LINK ) );
+
+ systemSettingManager.deleteSystemSetting( Setting.APPLICATION_INTRO );
+
+ assertNull( systemSettingManager.getSystemSetting( Setting.APPLICATION_INTRO ) );
+ assertEquals( "valueB", systemSettingManager.getSystemSetting( Setting.HELP_PAGE_LINK ) );
+
+ systemSettingManager.deleteSystemSetting( Setting.HELP_PAGE_LINK );
+
+ assertNull( systemSettingManager.getSystemSetting( Setting.APPLICATION_INTRO ) );
+ assertEquals( Setting.HELP_PAGE_LINK.getDefaultValue(), systemSettingManager.getSystemSetting( Setting.HELP_PAGE_LINK ) );
+ }
+
+ @Test
public void testGetAllSystemSettings()
{
systemSettingManager.saveSystemSetting( "settingA", "valueA" );