You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/04/20 21:42:12 UTC

svn commit: r395682 - /incubator/roller/trunk/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java

Author: agilliland
Date: Thu Apr 20 12:42:11 2006
New Revision: 395682

URL: http://svn.apache.org/viewcvs?rev=395682&view=rev
Log:
Adding custom L3 cache mapping for lookup of WeblogEntry by anchor.


Modified:
    incubator/roller/trunk/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java

Modified: incubator/roller/trunk/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java?rev=395682&r1=395681&r2=395682&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java (original)
+++ incubator/roller/trunk/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java Thu Apr 20 12:42:11 2006
@@ -27,6 +27,7 @@
 import java.util.Calendar;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -51,6 +52,9 @@
     
     private HibernatePersistenceStrategy strategy = null;
     
+    // cached mapping of entryAnchors -> entryIds
+    private Hashtable entryAnchorToIdMap = new Hashtable();
+    
     /* inline creation of reverse comparator, anonymous inner class */
     private Comparator reverseComparator = new ReverseComparator();
     
@@ -204,6 +208,9 @@
         
         // remove entry
         this.strategy.remove(entry);
+        
+        // remove entry from cache mapping
+        this.entryAnchorToIdMap.remove(entry.getWebsite().getHandle()+":"+entry.getAnchor());
     }
     
     
@@ -424,15 +431,34 @@
         }
     }
     
-    public WeblogEntryData getWeblogEntryByAnchor(
-            WebsiteData website, String anchor) throws RollerException {
+    
+    public WeblogEntryData getWeblogEntryByAnchor(WebsiteData website, String anchor) 
+            throws RollerException {
+        
         if (website == null)
             throw new RollerException("Website is null");
         
         if (anchor == null)
             throw new RollerException("Anchor is null");
         
+        // mapping key is combo of weblog + anchor
+        String mappingKey = website.getHandle()+":"+anchor;
+        
+        // check cache first
+        // NOTE: if we ever allow changing anchors then this needs updating
+        if(this.entryAnchorToIdMap.containsKey(mappingKey)) {
+            
+            WeblogEntryData entry = this.getWeblogEntry((String) this.entryAnchorToIdMap.get(mappingKey));
+            if(entry != null) {
+                log.debug("entryAnchorToIdMap CACHE HIT - "+mappingKey);
+                return entry;
+            } else {
+                // mapping hit with lookup miss?  mapping must be old, remove it
+                this.entryAnchorToIdMap.remove(mappingKey);
+            }
+        }
         
+        // cache failed, do lookup
         try {
             Session session = ((HibernatePersistenceStrategy)this.strategy).getSession();
             Criteria criteria = session.createCriteria(WeblogEntryData.class);
@@ -443,7 +469,19 @@
             criteria.setMaxResults(1);
             
             List list = criteria.list();
-            return list.size()!=0 ? (WeblogEntryData)list.get(0) : null;
+            
+            WeblogEntryData entry = null;
+            if(list.size() != 0) {
+                entry = (WeblogEntryData) criteria.uniqueResult();
+            }
+            
+            // add mapping to cache
+            if(entry != null) {
+                log.debug("entryAnchorToIdMap CACHE MISS - "+mappingKey);
+                this.entryAnchorToIdMap.put(mappingKey, entry.getId());
+            }
+            
+            return entry;
         } catch (HibernateException e) {
             throw new RollerException(e);
         }