diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java @ 1675:19d0eb41e923

AnnotationArtifact: Bugfixes and enabled cache support. flys-artifacts/trunk@2892 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 05 Oct 2011 10:26:54 +0000
parents 26e19cdaed5e
children 5d8b3880a553
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Tue Oct 04 16:36:19 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Wed Oct 05 10:26:54 2011 +0000
@@ -8,7 +8,7 @@
 
 import org.apache.log4j.Logger;
 
-//import net.sf.ehcache.Cache;
+import net.sf.ehcache.Cache;
 
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactNamespaceContext;
@@ -38,13 +38,13 @@
 public class AnnotationArtifact extends StaticFLYSArtifact {
 
     /** The logger for this class. */
-    private static Logger logger = Logger.getLogger(WINFOArtifact.class);
+    private static Logger logger = Logger.getLogger(AnnotationArtifact.class);
 
     /** The name of the artifact. */
     public static final String ARTIFACT_NAME = "annotation";
 
     /* Name of cache. */
-    //public static final String CACHE_NAME = "annotations";
+    public static final String CACHE_NAME = "annotations";
 
     @Override
     protected void initialize(Artifact artifact, Object context,
@@ -226,12 +226,33 @@
      */
     public List<Annotation> getAnnotations() {
         String river = FLYSUtils.getRiver(this).getName();
-        logger.debug("Search annotations for river: " /*+ river*/);
-        //Cache cache = CacheFactory.getCache(CACHE_NAME);
+        logger.debug("Search annotations for river: " + river);
 
-        List<Annotation> annotations = new ArrayList<Annotation>();
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+        String  key = river;
+        Object  old = null;
 
-        return getAnnotationsUncached(river);
+        if (cache != null) {
+            logger.debug("We are using a cache for annotations.");
+
+            net.sf.ehcache.Element element = cache.get(key);
+            if (element != null) {
+                logger.info("Fetched annotations from cache.");
+                old = element.getValue();
+            }
+        }
+
+        if (old == null) {
+            old = getAnnotationsUncached(river);
+        }
+
+        if (cache != null && old != null) {
+            cache.put(new net.sf.ehcache.Element(key, old));
+        }
+
+        return old != null
+            ? (List<Annotation>) old
+            : new ArrayList<Annotation>();
     }
 
     /**
@@ -241,6 +262,8 @@
      * @see DistanceInfoService to access cached documents.
      */
     protected List<Annotation> getAnnotationsUncached(String river) {
+        logger.info("Fetch annotations from database.");
+
         List<Annotation> annotations = new ArrayList<Annotation>();
         annotations = AnnotationsFactory.getPointAnnotations(river);
 

http://dive4elements.wald.intevation.org