← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16986: CalendarService. Added a memory cache (map) around the call to SystemSettingManager. This to avoi...

 

------------------------------------------------------------
revno: 16986
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-10-07 19:30:40 +0200
message:
  CalendarService. Added a memory cache (map) around the call to SystemSettingManager. This to avoid call to a transactional method which is expensive especially insisde loops.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/CalendarService.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/calendar/DefaultCalendarService.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/SystemSettingInterceptor.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetGeneralSettingsAction.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/calendar/CalendarService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/CalendarService.java	2014-04-29 10:27:58 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/CalendarService.java	2014-10-07 17:30:40 +0000
@@ -37,6 +37,9 @@
  */
 public interface CalendarService
 {
+    final String KEY_CALENDAR = "keyCalendar";
+    final String KEY_DATE_FORMAT = "keyDateFormat";
+    
     /**
      * Gets all available calendars as a sorted list.
      * @return All available calendars
@@ -61,4 +64,28 @@
      * @see DateFormat
      */
     DateFormat getSystemDateFormat();
+    
+    /**
+     * Gets the system calendar key.
+     * @return the system calendar key
+     */
+    String getSystemCalendarKey();
+    
+    /**
+     * Sets the system calendar key.
+     * @param calendarKey the system calendar key
+     */
+    void setSystemCalendarKey( String calendarKey );
+    
+    /**
+     * Gets the system date format key.
+     * @return the system date format key
+     */
+    String getSystemDateFormatKey();
+    
+    /**
+     * Sets the system date format key.
+     * @param dateFormatKey the system date format key
+     */
+    void setSystemDateFormatKey( String dateFormatKey );
 }

=== 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	2014-08-26 07:05:04 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java	2014-10-07 17:30:40 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.calendar.CalendarService;
 import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.QuarterlyPeriodType;
 import org.hisp.dhis.period.YearlyPeriodType;
@@ -51,8 +52,6 @@
     final String KEY_APPLICATION_INTRO = "keyApplicationIntro";
     final String KEY_APPLICATION_NOTIFICATION = "keyApplicationNotification";
     final String KEY_APPLICATION_FOOTER = "keyApplicationFooter";
-    final String KEY_CALENDAR = "keyCalendar";
-    final String KEY_DATE_FORMAT = "keyDateFormat";
     final String KEY_FLAG = "keyFlag";
     final String KEY_FLAG_IMAGE = "keyFlagImage";
     final String KEY_START_MODULE = "startModule";
@@ -128,8 +127,8 @@
         {
             put( KEY_SCHEDULE_AGGREGATE_QUERY_BUILDER_TASK_STRATEGY, DEFAULT_SCHEDULE_AGGREGATE_QUERY_BUILDER_TASK_STRATEGY );
             put( KEY_FLAG, DEFAULT_FLAG );
-            put( KEY_CALENDAR, DEFAULT_CALENDAR );
-            put( KEY_DATE_FORMAT, DEFAULT_DATE_FORMAT );
+            put( CalendarService.KEY_CALENDAR, DEFAULT_CALENDAR );
+            put( CalendarService.KEY_DATE_FORMAT, DEFAULT_DATE_FORMAT );
             put( KEY_FACTOR_OF_DEVIATION, DEFAULT_FACTOR_OF_DEVIATION );
             put( KEY_ORGUNITGROUPSET_AGG_LEVEL, DEFAULT_ORGUNITGROUPSET_AGG_LEVEL );
             put( KEY_START_MODULE, DEFAULT_START_MODULE );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/calendar/DefaultCalendarService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/calendar/DefaultCalendarService.java	2014-05-21 07:25:28 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/calendar/DefaultCalendarService.java	2014-10-07 17:30:40 +0000
@@ -28,25 +28,29 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.PostConstruct;
+
 import org.hisp.dhis.calendar.impl.Iso8601Calendar;
 import org.hisp.dhis.period.Cal;
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.setting.SystemSettingManager;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import javax.annotation.PostConstruct;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public class DefaultCalendarService implements CalendarService
+public class DefaultCalendarService 
+    implements CalendarService
 {
     @Autowired
     private SystemSettingManager settingManager;
@@ -61,6 +65,15 @@
         new DateFormat( "dd-MM-yyyy", "dd-MM-yyyy", "dd-MM-yyyy", "dd-mm-yyyy" )
     );
 
+    /**
+     * Memory cache for calendar and date format keys.
+     */
+    private final Map<String, String> keyCache = new HashMap<>();
+
+    // -------------------------------------------------------------------------
+    // CalendarService implementation
+    // -------------------------------------------------------------------------
+
     @PostConstruct
     public void init()
     {
@@ -91,8 +104,8 @@
     @Override
     public Calendar getSystemCalendar()
     {
-        String calendarKey = (String) settingManager.getSystemSetting( SystemSettingManager.KEY_CALENDAR, SystemSettingManager.DEFAULT_CALENDAR );
-        String dateFormat = (String) settingManager.getSystemSetting( SystemSettingManager.KEY_DATE_FORMAT, SystemSettingManager.DEFAULT_DATE_FORMAT );
+        String calendarKey = getSystemCalendarKey();
+        String dateFormat = getSystemDateFormatKey();
 
         Calendar calendar;
 
@@ -109,11 +122,11 @@
 
         return calendar;
     }
-
+    
     @Override
     public DateFormat getSystemDateFormat()
     {
-        String dateFormatKey = (String) settingManager.getSystemSetting( SystemSettingManager.KEY_DATE_FORMAT, SystemSettingManager.DEFAULT_DATE_FORMAT );
+        String dateFormatKey = getSystemDateFormatKey();
 
         for ( DateFormat dateFormat : dateFormats )
         {
@@ -125,4 +138,36 @@
 
         return dateFormats.get( 0 );
     }
+
+    // -------------------------------------------------------------------------
+    // Calendar key
+    // -------------------------------------------------------------------------
+
+    public String getSystemCalendarKey()
+    {
+        String key = keyCache.containsKey( KEY_CALENDAR ) ? keyCache.get( KEY_CALENDAR ) : 
+            (String) settingManager.getSystemSetting( KEY_CALENDAR, SystemSettingManager.DEFAULT_CALENDAR );        
+        keyCache.put( KEY_CALENDAR, key );        
+        return key;
+    }
+    
+    public void setSystemCalendarKey( String calendarKey )
+    {
+        keyCache.put( KEY_CALENDAR, calendarKey );
+        settingManager.saveSystemSetting( KEY_CALENDAR, calendarKey );
+    }
+    
+    public String getSystemDateFormatKey()
+    {
+        String key = keyCache.containsKey( KEY_DATE_FORMAT ) ? keyCache.get( KEY_DATE_FORMAT ) :
+            (String) settingManager.getSystemSetting( KEY_DATE_FORMAT, SystemSettingManager.DEFAULT_DATE_FORMAT );
+        keyCache.put( KEY_DATE_FORMAT, key );
+        return key;
+    }
+
+    public void setSystemDateFormatKey( String dateFormatKey )
+    {
+        keyCache.put( KEY_DATE_FORMAT, dateFormatKey );
+        settingManager.saveSystemSetting( KEY_DATE_FORMAT, dateFormatKey );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/SystemSettingInterceptor.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/SystemSettingInterceptor.java	2014-08-26 07:05:04 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/SystemSettingInterceptor.java	2014-10-07 17:30:40 +0000
@@ -88,8 +88,9 @@
     {
         Map<String, Object> map = new HashMap<>();
 
-        map.put( KEY_CALENDAR, systemSettingManager.getSystemSetting( KEY_CALENDAR, DEFAULT_CALENDAR ) );
-        map.put( KEY_DATE_FORMAT, systemSettingManager.getSystemSetting( KEY_DATE_FORMAT, DEFAULT_DATE_FORMAT ) );
+        map.put( CalendarService.KEY_CALENDAR, calendarService.getSystemCalendarKey() );
+        map.put( CalendarService.KEY_DATE_FORMAT, calendarService.getSystemDateFormatKey() );
+        
         map.put( DATE_FORMAT, calendarService.getSystemDateFormat() );
         map.put( KEY_CACHE_STRATEGY, systemSettingManager.getSystemSetting( KEY_CACHE_STRATEGY, DEFAULT_CACHE_STRATEGY ) );
         map.put( KEY_ANALYTICS_MAX_LIMIT, systemSettingManager.getSystemSetting( KEY_ANALYTICS_MAX_LIMIT, DEFAULT_ANALYTICS_MAX_LIMIT ) );

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetGeneralSettingsAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetGeneralSettingsAction.java	2014-08-27 09:20:22 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetGeneralSettingsAction.java	2014-10-07 17:30:40 +0000
@@ -31,6 +31,7 @@
 import com.opensymphony.xwork2.Action;
 
 import org.apache.commons.lang3.StringUtils;
+import org.hisp.dhis.calendar.CalendarService;
 import org.hisp.dhis.configuration.Configuration;
 import org.hisp.dhis.configuration.ConfigurationService;
 import org.hisp.dhis.dataelement.DataElementService;
@@ -40,6 +41,7 @@
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.setting.SystemSettingManager;
 import org.hisp.dhis.user.UserGroupService;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import static org.hisp.dhis.setting.SystemSettingManager.*;
 
@@ -95,6 +97,9 @@
     {
         this.organisationUnitService = organisationUnitService;
     }
+    
+    @Autowired
+    private CalendarService calendarService;
 
     // -------------------------------------------------------------------------
     // Output
@@ -240,11 +245,12 @@
         systemSettingManager.saveSystemSetting( KEY_PHONE_NUMBER_AREA_CODE, phoneNumberAreaCode );
         systemSettingManager.saveSystemSetting( KEY_MULTI_ORGANISATION_UNIT_FORMS, multiOrganisationUnitForms );
         systemSettingManager.saveSystemSetting( KEY_GOOGLE_ANALYTICS_UA, googleAnalyticsUA );
-        systemSettingManager.saveSystemSetting( KEY_CALENDAR, calendar );
-        systemSettingManager.saveSystemSetting( KEY_DATE_FORMAT, dateFormat );
         systemSettingManager.saveSystemSetting( KEY_ANALYTICS_MAINTENANCE_MODE, analyticsMaintenanceMode );
         systemSettingManager.saveSystemSetting( KEY_HELP_PAGE_LINK, StringUtils.trimToNull( helpPageLink ) );
 
+        calendarService.setSystemCalendarKey( calendar );
+        calendarService.setSystemDateFormatKey( dateFormat );
+        
         Configuration configuration = configurationService.getConfiguration();
 
         if ( feedbackRecipients != null )