[cvs] expresso commit by lhamel: add getImmutableCachedObject() for direct

JCorporate Ltd jcorp at jcorporate.com
Mon Feb 21 01:09:45 UTC 2005


Log Message:
-----------
add getImmutableCachedObject() for direct access to cached item

Modified Files:
--------------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj:
        CacheUtils.java

Revision Data
-------------
Index: CacheUtils.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/CacheUtils.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/CacheUtils.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/CacheUtils.java -u -r1.13 -r1.14
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/CacheUtils.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/CacheUtils.java
@@ -67,14 +67,10 @@
 import com.jcorporate.expresso.core.cache.CacheException;
 import com.jcorporate.expresso.core.cache.CacheManager;
 import com.jcorporate.expresso.core.cache.CacheSystem;
-import com.jcorporate.expresso.core.dataobjects.DataField;
-import com.jcorporate.expresso.core.dataobjects.DefaultDataField;
 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData;
 import com.jcorporate.expresso.core.db.DBException;
 import org.apache.log4j.Logger;
 
-import java.util.Iterator;
-
 
 /**
  * Utility methods for putting a DBObject into a cache.
@@ -129,16 +125,13 @@
                 cs.removeItem(theDBObj.getClass().getName(), theDBObj);
 //				newObj = prepareUpdateFieldsForStorage(oldDBObj, theDBObj, metadata);
             } else {
-                newObj = prepareForStorage(theDBObj, metadata);
-
-
+                newObj = prepareForStorage(theDBObj);
                 Integer i = (Integer) theDBObj.getAttribute("TTL");
                 if (i == null || i.intValue() == 0) {
                     cs.put(theClassName, newObj);
                 } else {
                     //Set the cache value.
-                    cs.put(theClassName, newObj,
-                            i.intValue() * 1000 * 60);
+                    cs.put(theClassName, newObj, i.intValue() * 1000 * 60);
                 }
 
             }
@@ -163,7 +156,6 @@
      */
     public void addUnmodifiedToCache(DBObject theDBObj) throws DBException {
         JDBCObjectMetaData metadata = theDBObj.getJDBCMetaData();
-        String theClassName = theDBObj.getClass().getName();
 
         //
         //Don't cache anything without key fields
@@ -185,17 +177,16 @@
             return;
         }
         try {
+            String theClassName = theDBObj.getClass().getName();
             prepareCache(cs, cacheSize, theClassName);
-            DBObject newObj = prepareForStorage(theDBObj, metadata);
-
+            DBObject newObj = prepareForStorage(theDBObj);
 
             Integer i = (Integer) theDBObj.getAttribute("TTL");
             if (i == null || i.intValue() == 0) {
                 cs.put(theClassName, newObj);
             } else {
                 //Set the cache value.
-                cs.put(theClassName, newObj,
-                        i.intValue() * 1000 * 60);
+                cs.put(theClassName, newObj, i.intValue() * 1000 * 60);
             }
 
 
@@ -248,7 +239,7 @@
         }
         try {
             prepareCache(cs, cacheSize, theClassName);
-            DBObject newObj = prepareForStorage(theDBObj, metadata);
+            DBObject newObj = prepareForStorage(theDBObj);
 
 
             Integer i = (Integer) theDBObj.getAttribute("TTL");
@@ -256,8 +247,7 @@
                 cs.addItem(theClassName, newObj);
             } else {
                 //Set the cache value.
-                cs.addItem(theClassName, newObj,
-                        i.intValue() * 1000 * 60);
+                cs.addItem(theClassName, newObj, i.intValue() * 1000 * 60);
             }
 
 
@@ -280,8 +270,7 @@
      */
     private void prepareCache(CacheSystem cs, int cacheSize, String cacheName) throws CacheException {
         if (!cs.existsCache(cacheName)) {
-            cs.createCache(cacheName, false,
-                    cacheSize);
+            cs.createCache(cacheName, false, cacheSize);
         }
 
     }
@@ -290,29 +279,16 @@
      * Creates a DBObject for storage in a cache, prepares the cache storage
      *
      * @param theDBObj the source object
-     * @param metadata the object's metadata
      * @return DBObject instance ready to store
      * @throws DBException upon DBObject access error
      */
-    private DBObject prepareForStorage(DBObject theDBObj,
-                                       JDBCObjectMetaData metadata)
+    private DBObject prepareForStorage(DBObject theDBObj)
             throws DBException {
 
         DBObject newObj = theDBObj.newInstance();
         newObj.setDataContext(theDBObj.getDataContext());
-
-        DBField oneField = null;
-
-        for (Iterator i = metadata.getAllFieldsMap().values().iterator();
-             i.hasNext();) {
-            oneField = (DBField) i.next();
-
-            if (!oneField.isVirtual() && !oneField.isBinaryObjectType()) {
-                DataField df = DefaultDataField.getInstance(oneField, newObj);
-                df.setValue(theDBObj.getDataField(oneField.getName()).getValue());
-                newObj.setDataField(oneField.getName(), df);
-            }
-        } /* for each field */
+        newObj.copyAllFields(theDBObj);
+        newObj.isMutable(false); // cached copy cannot be changed
         return newObj;
     }
 
@@ -354,4 +330,41 @@
 //	        }
 //	    } /* for each field */
 //	    return theDBObj; }
+
+
+    /**
+     * get actual cached object, which is flagged to be immutable.  efficient for read-only
+     * situations where you want to avoid making a new copy of the object.
+     *
+     * @see DBObject#getKey()
+     * @param dbobjClass class of desired DBObject
+     * @param keyValues String combination of all PK values (see getKey())
+     */
+    public DBObject getImmutableCachedObject(Class dbobjClass, String keyValues) {
+
+        DBObject result = null;
+        if ( keyValues == null || keyValues.length() == 0 ) {
+            return result;
+        }
+        if ( dbobjClass == null ) {
+            return result;
+        }
+
+        CacheSystem cs = CacheManager.getCacheSystem(null); // will use RequestRegistry
+        String cacheName = dbobjClass.getName();
+        result = (DBObject) cs.getItem(cacheName, keyValues);
+        if ( result == null ) {
+            try {
+                DBObject o = (DBObject) dbobjClass.newInstance();
+                o.setKey(keyValues);
+                if ( o.find() ) {
+                    // find will automatically cache a new object; that cached value is what we want
+                    result = (DBObject) cs.getItem(cacheName, keyValues);
+                }
+            } catch (Exception e) {
+                log.error("Cannot instantiate class: " + dbobjClass.getName(), e);
+            }
+        }
+        return result;
+    }
 }


More information about the cvs mailing list