changeset 374:91fbaa2744bf

Added caching support for the wst value tables. flys-artifacts/trunk@1783 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 01 May 2011 10:58:38 +0000
parents 7f7d6037d242
children 60f63539d004
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java
diffstat 3 files changed, 69 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Apr 29 16:56:37 2011 +0000
+++ b/flys-artifacts/ChangeLog	Sun May 01 10:58:38 2011 +0000
@@ -1,3 +1,11 @@
+2011-05-01	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java:
+	  Use the cache for the wst value table if configured.
+
+	* doc/conf/cache.xml: Choose a more precise name for the 
+	  wst value table cache.
+
 2011-04-29	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* pom.xml: Added dependency to Ehcache. Apache 2.0 license.
--- a/flys-artifacts/doc/conf/cache.xml	Fri Apr 29 16:56:37 2011 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Sun May 01 10:58:38 2011 +0000
@@ -17,7 +17,7 @@
 
     <!-- This one is used for the WST value tables -->
 
-    <cache name="wst-cache"
+    <cache name="wst-value-table"
            maxElementsInMemory="20"
            maxElementsOnDisk="100"
            eternal="false"
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Fri Apr 29 16:56:37 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Sun May 01 10:58:38 2011 +0000
@@ -6,6 +6,8 @@
 import de.intevation.flys.model.Wst;
 import de.intevation.flys.model.WstColumn;
 
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
 import de.intevation.flys.backend.SessionHolder;
 
 import java.util.Arrays;
@@ -28,11 +30,16 @@
 
 import org.hibernate.type.StandardBasicTypes;
 
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
 public class WstValueTable
 implements   Serializable
 {
     private static Logger log = Logger.getLogger(WstValueTable.class);
 
+    public static final String CACHE_NAME = "wst-value-table";
+
     // TODO: put this into a property file
     public static final String SQL_POS_WQ = 
         "SELECT position, w, q, column_pos" +
@@ -410,8 +417,61 @@
         return getTable(river, 0);
     }
 
+    public static final class CacheKey
+    implements                Serializable
+    {
+        private int riverId;
+        private int kind;
+
+        public CacheKey(int riverId, int kind) {
+            this.riverId = riverId;
+            this.kind    = kind;
+        }
+
+        public int hashCode() {
+            return (riverId << 8) | kind;
+        }
+
+        public boolean equals(Object other) {
+            if (!(other instanceof CacheKey)) {
+                return false;
+            }
+            CacheKey o = (CacheKey)other;
+            return riverId == o.riverId && kind == o.kind;
+        }
+    } // class CacheKey
+
     public static WstValueTable getTable(River river, int kind) {
 
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        CacheKey cacheKey;
+
+        if (cache != null) {
+            cacheKey = new CacheKey(river.getId(), kind);
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                log.debug("got wst value table from cache");
+                return (WstValueTable)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        WstValueTable valueTable = getTableUncached(river, kind);
+
+        if (cacheKey != null) {
+            log.debug("store wst value in cache");
+            Element element = new Element(cacheKey, valueTable);
+            cache.put(element);
+        }
+
+        return valueTable;
+    }
+
+    public static WstValueTable getTableUncached(River river, int kind) {
+
         Session session = SessionHolder.HOLDER.get();
 
         Query query = session.createQuery(

http://dive4elements.wald.intevation.org