← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17406: Add USER_APPROVAL_LEVEL_CACHE to DataApprovalPermissionsEvaluator

 

------------------------------------------------------------
revno: 17406
committer: jimgrace@xxxxxxxxx
branch nick: dhis2
timestamp: Mon 2014-11-10 14:18:02 -0500
message:
  Add USER_APPROVAL_LEVEL_CACHE to DataApprovalPermissionsEvaluator
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DataApprovalPermissionsEvaluator.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-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DataApprovalPermissionsEvaluator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DataApprovalPermissionsEvaluator.java	2014-11-10 13:03:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DataApprovalPermissionsEvaluator.java	2014-11-10 19:18:02 +0000
@@ -31,10 +31,17 @@
 import static org.hisp.dhis.setting.SystemSettingManager.KEY_ACCEPTANCE_REQUIRED_FOR_APPROVAL;
 import static org.hisp.dhis.setting.SystemSettingManager.KEY_HIDE_UNAPPROVED_DATA_IN_ANALYTICS;
 
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.setting.SystemSettingManager;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
 /**
  * This package private class holds the context for deciding on data approval permissions.
  * The context contains both system settings and some qualities of the user.
@@ -65,6 +72,10 @@
     {
     }
 
+    private static Cache<String, DataApprovalLevel> USER_APPROVAL_LEVEL_CACHE = CacheBuilder.newBuilder()
+            .expireAfterAccess( 10, TimeUnit.MINUTES ).initialCapacity( 10000 )
+            .maximumSize( 50000 ).build();
+
     /**
      * Allocates and populates the context for determining user permissions
      * on one or more DataApproval objects.
@@ -112,7 +123,7 @@
      */
     DataApprovalPermissions getPermissions( DataApprovalStatus status )
     {
-        DataApproval da = status.getDataApproval();
+        final DataApproval da = status.getDataApproval();
 
         DataApprovalState s = status.getState();
 
@@ -125,7 +136,22 @@
             return permissions; // No permissions are set.
         }
 
-        DataApprovalLevel userApprovalLevel = dataApprovalLevelService.getUserApprovalLevel( user, da.getOrganisationUnit() );
+        DataApprovalLevel userApprovalLevel;
+
+        try
+        {
+            userApprovalLevel = ( USER_APPROVAL_LEVEL_CACHE.get( user.getId() + "-" + da.getOrganisationUnit().getId(), new Callable<DataApprovalLevel>()
+            {
+                public DataApprovalLevel call() throws ExecutionException
+                {
+                    return dataApprovalLevelService.getUserApprovalLevel( user, da.getOrganisationUnit() );
+                }
+            } ) );
+        }
+        catch ( ExecutionException ex )
+        {
+            throw new RuntimeException( ex );
+        }
 
         if ( userApprovalLevel == null )
         {