← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17013: switch to using Guava CacheBuilder for id => program cache in event import

 

------------------------------------------------------------
revno: 17013
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-10-09 13:53:32 +0700
message:
  switch to using Guava CacheBuilder for id => program cache in event import
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.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-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2014-10-09 06:32:38 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2014-10-09 06:53:32 +0000
@@ -30,6 +30,8 @@
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
@@ -82,6 +84,9 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -651,8 +656,7 @@
             }
         }
 
-        Collection<TrackedEntityDataValue> dataValues = dataValueService
-            .getTrackedEntityDataValues( programStageInstance );
+        Collection<TrackedEntityDataValue> dataValues = dataValueService.getTrackedEntityDataValues( programStageInstance );
 
         for ( TrackedEntityDataValue dataValue : dataValues )
         {
@@ -963,27 +967,30 @@
         return organisationUnit;
     }
 
-    private Map<String, Program> programMap = new HashMap<>();
+    private static Cache<String, Program> programCache = CacheBuilder.newBuilder()
+        .expireAfterAccess( 5, TimeUnit.MINUTES )
+        .initialCapacity( 10 )
+        .maximumSize( 50 )
+        .build();
 
-    private Program getProgram( String id )
+    private Program getProgram( final String id )
     {
-        Program program;
-
-        if ( programMap.containsKey( id ) )
-        {
-            program = programMap.get( id );
-        }
-        else
-        {
-            program = programService.getProgram( id );
-
-            if ( program != null )
+        try
+        {
+            return programCache.get( id, new Callable<Program>()
             {
-                programMap.put( id, program );
-            }
+                @Override
+                public Program call() throws Exception
+                {
+                    return programService.getProgram( id );
+                }
+            } );
+        }
+        catch ( ExecutionException ignored )
+        {
         }
 
-        return program;
+        return null;
     }
 
     private ProgramStage getProgramStage( String id )