a first stab at a memory resident MRU cache. very untested, just checking it in if...
authorjohn <john>
Thu, 17 Apr 2003 22:33:50 +0000 (22:33 +0000)
committerjohn <john>
Thu, 17 Apr 2003 22:33:50 +0000 (22:33 +0000)
look at the way i'm planning on doing it.

source/mircoders/global/CacheKey.java [new file with mode: 0755]
source/mircoders/global/MRUCache.java [new file with mode: 0755]
source/mircoders/global/MirGlobal.java

diff --git a/source/mircoders/global/CacheKey.java b/source/mircoders/global/CacheKey.java
new file mode 100755 (executable)
index 0000000..30b7e21
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with the com.oreilly.servlet library, any library
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of
+ * the above that use the same license as the above), and distribute linked
+ * combinations including the two.  You must obey the GNU General Public
+ * License in all respects for all of the code used other than the above
+ * mentioned libraries.  If you modify this file, you may extend this exception
+ * to your version of the file, but you are not obligated to do so.  If you do
+ * not wish to do so, delete this exception statement from your version.
+ */
+
+package mircoders.global;
+
+
+public class CacheKey {
+  public String type;
+  public String selector;
+
+  public CacheKey(String theType,String theSelector){
+    type=theType;
+    selector=theSelector;
+  }
+  public boolean equals(CacheKey aCacheKey){
+    if (aCacheKey.type.equals(type) && aCacheKey.selector.equals(selector))
+      return true;
+    else
+      return false;
+    
+  }
+}
diff --git a/source/mircoders/global/MRUCache.java b/source/mircoders/global/MRUCache.java
new file mode 100755 (executable)
index 0000000..8e8a415
--- /dev/null
@@ -0,0 +1,123 @@
+/*\r
+ * Copyright (C) 2001, 2002  The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with the com.oreilly.servlet library, any library\r
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
+ * the above that use the same license as the above), and distribute linked\r
+ * combinations including the two.  You must obey the GNU General Public\r
+ * License in all respects for all of the code used other than the above\r
+ * mentioned libraries.  If you modify this file, you may extend this exception\r
+ * to your version of the file, but you are not obligated to do so.  If you do\r
+ * not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mircoders.global;\r
+\r
+\r
+import java.util.HashMap;\r
+import java.util.LinkedList;\r
+import java.util.Map;\r
+\r
+import mir.config.MirPropertiesConfiguration;\r
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
+import mir.log.LoggerWrapper;\r
+\r
+import mircoders.global.CacheKey;\r
+\r
+public class MRUCache {\r
+  private Map cache;\r
+  private LinkedList mruList;\r
+  private int cacheMaxItems;\r
+\r
+  private MirPropertiesConfiguration configuration;\r
+  private LoggerWrapper logger;\r
+  \r
+\r
+\r
+  public MRUCache() {\r
+    logger = new LoggerWrapper("Global.MRUCache");\r
+    try {\r
+      configuration = MirPropertiesConfiguration.instance();\r
+       }\r
+    catch (PropertiesConfigExc e) {\r
+      throw new RuntimeException("Can't get configuration: " + e.getMessage());\r
+    }\r
+    cacheMaxItems=Integer.parseInt(configuration.getString("Global.Cache.Items"));\r
+    cache = new HashMap();\r
+    mruList= new LinkedList();\r
+    \r
+  }\r
+\r
+  /**\r
+   * Checks if the cache has an object with the specified key  \r
+   */\r
+\r
+  public boolean hasObject(CacheKey aCacheKey) {\r
+    synchronized (cache) {\r
+      return cache.containsKey(aCacheKey);\r
+    }\r
+  }\r
+  \r
+  /**\r
+   * Stores an object in the cache by placing it at the top of the\r
+   * list If the object is in the cache, it promotes it to the top of\r
+   * the list.  If the object is not in the cache, it adds it to the\r
+   * top of the list, and then checks the max size of the cache versus\r
+   * the new size to see if it needs to remove the last element from\r
+   * the cache.\r
+   */\r
+\r
+  public void storeObject(CacheKey aCacheKey,Object data) {\r
+    synchronized (cache) {\r
+      if (! hasObject(aCacheKey)){\r
+       // add to the cache\r
+       cache.put(aCacheKey,data);\r
+       if (mruList.size() > cacheMaxItems){\r
+         removeObject((CacheKey) mruList.getLast());\r
+       }\r
+      }\r
+      mruList.remove(aCacheKey);\r
+      mruList.addFirst(aCacheKey);\r
+    }\r
+  }  \r
+  \r
+  public void removeObject(CacheKey aCacheKey){\r
+    synchronized (cache) {\r
+      mruList.remove(aCacheKey);\r
+      cache.remove(aCacheKey);\r
+    }\r
+  }\r
+  \r
+  /**\r
+   * Moves requested item to front of cache\r
+   */\r
+\r
+  public Object getObject(CacheKey aCacheKey){\r
+    synchronized (cache) {\r
+      mruList.remove(aCacheKey);\r
+      mruList.addFirst(aCacheKey);\r
+      return cache.get(aCacheKey);\r
+    }\r
+  }\r
+}\r
+\r
+\r
+\r
index 2e29c9d..a28888f 100755 (executable)
@@ -42,6 +42,7 @@ public class MirGlobal {
   static private MirLocalizer localizer;
   static private ProducerEngine producerEngine;
   static private Abuse abuse;
+  static private MRUCache mruCache;
 
   public static MirLocalizer localizer() {
     String localizerClassName;
@@ -102,4 +103,15 @@ public class MirGlobal {
 
     return producerEngine;
   }
+
+  public static MRUCache mruCache() {
+    synchronized(MirGlobal.class) {
+      if (mruCache == null) {
+       mruCache = new MRUCache();
+      }
+      return mruCache;
+    }
+  }
 }
+