ozeigermann    2004/10/20 00:22:44

  Modified:    src/share/org/apache/slide/util TxLRUObjectCache.java
                        ByteSizeLimitedObjectCache.java
               src/share/org/apache/slide/store ExtendedStore.java
  Log:
  Added configurable timeouts to the caches in ExtendedStore
  
  Revision  Changes    Path
  1.8       +69 -8     
jakarta-slide/src/share/org/apache/slide/util/TxLRUObjectCache.java
  
  Index: TxLRUObjectCache.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/util/TxLRUObjectCache.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TxLRUObjectCache.java     19 Oct 2004 11:40:33 -0000      1.7
  +++ TxLRUObjectCache.java     20 Oct 2004 07:22:43 -0000      1.8
  @@ -115,7 +115,7 @@
               }
   
               Map changeCache = (Map) txChangeCaches.get(txId);
  -            Object changed = changeCache.get(key);
  +            Object changed = getFromLocalCache(changeCache, key);
               if (changed != null) {
                   hit(txId, key);
                   // if object has been changed in this tx, get the local one
  @@ -129,7 +129,7 @@
           if (noGlobalCachingInsideTx && txId != null) return null;
           
           // as fall back return value from global cache (if present)
  -        Object global = globalCache.get(key);
  +        Object global = getFromGlobalCache(key);
           if (global != null) {
               hit(txId, key);
           } else {
  @@ -138,6 +138,13 @@
           return global;
       }
   
  +    public synchronized void put(Object txId, Object key, Object value, long 
timeout) {
  +        if (timeout != -1) {
  +            value = new TimeoutValuePair(value, timeout);
  +        }
  +        put(txId, key, value);
  +    }
  +
       public synchronized void put(Object txId, Object key, Object value) {
           if (txId != null) {
               // if it has been deleted before, undo this
  @@ -152,7 +159,7 @@
               }
           } else {
               if (globalCache != null) {
  -                globalCache.put(key, value);
  +                putToGlobalCache(key, value);
                   if (loggingEnabled) {
                       logger.log("Added '" + key + "'", logChannel, Logger.DEBUG);
                   }
  @@ -263,7 +270,7 @@
                   Map changeCache = (Map) txChangeCaches.get(txId);
                   for (Iterator it = changeCache.entrySet().iterator(); 
it.hasNext();) {
                       Map.Entry entry = (Map.Entry) it.next();
  -                    globalCache.put(entry.getKey(), entry.getValue());
  +                    putToGlobalCache(entry.getKey(), entry.getValue());
                   }
   
                   Set deleteCache = (Set) txDeleteCaches.get(txId);
  @@ -329,6 +336,38 @@
           }
       }
       
  +    protected void putToGlobalCache(Object key, Object value) {
  +        if (value instanceof TimeoutValuePair) {
  +            // start the timeout process as soon as the entry is pushed to the 
global store
  +            ((TimeoutValuePair) value).activate();
  +        }
  +        globalCache.put(key, value);
  +    }
  +    
  +    protected Object getFromGlobalCache(Object key) {
  +        Object value = globalCache.get(key);
  +        
  +        if (value != null && value instanceof TimeoutValuePair) {
  +            if (!((TimeoutValuePair) value).isValid()) {
  +                globalCache.remove(key);
  +                value = null;
  +            } else {
  +                value = ((TimeoutValuePair) value).value;
  +            }
  +        }
  +        return value;
  +    }
  +    
  +    protected Object getFromLocalCache(Map cache, Object key) {
  +        Object value = cache.get(key);
  +
  +        // XXX local caches never time out
  +        if (value != null && value instanceof TimeoutValuePair) {
  +            value = ((TimeoutValuePair) value).value;
  +        }
  +        return value;
  +    }
  +    
       protected void prune(Map map, Object key, String delimiter) {
           String prefix = key + delimiter;
           for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
  @@ -350,6 +389,28 @@
                       set.add(entry.getKey());
                   }
               }
  +        }
  +    }
  +    
  +    static class TimeoutValuePair {
  +
  +        Object value;
  +        long invalidationTime;
  +        long timeOut;
  +        
  +        TimeoutValuePair(Object value, long timeOut) {
  +            this.value = value;
  +            this.timeOut = timeOut;
  +            this.invalidationTime = -1;
  +        }
  +        
  +        void activate() {
  +            invalidationTime = timeOut + System.currentTimeMillis();
  +        }
  +        
  +        boolean isValid() {
  +            long currentTime = System.currentTimeMillis();
  +            return (invalidationTime > currentTime);
           }
       }
   }
  
  
  
  1.6       +13 -5     
jakarta-slide/src/share/org/apache/slide/util/ByteSizeLimitedObjectCache.java
  
  Index: ByteSizeLimitedObjectCache.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/util/ByteSizeLimitedObjectCache.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ByteSizeLimitedObjectCache.java   28 Jul 2004 09:34:29 -0000      1.5
  +++ ByteSizeLimitedObjectCache.java   20 Oct 2004 07:22:44 -0000      1.6
  @@ -29,6 +29,7 @@
   import java.util.Set;
   import java.util.Iterator;
   
  +import org.apache.slide.util.TxLRUObjectCache.TimeoutValuePair;
   import org.apache.slide.util.logger.Logger;
   
   import org.apache.commons.collections.LRUMap;
  @@ -110,6 +111,13 @@
           return (maxSize >= byteSize && maxByteSizePerEntry >= byteSize);
       }
   
  +    public synchronized void put(Object txId, Object key, Object value, long 
byteSize, long timeout) {
  +        if (timeout != -1) {
  +            value = new TimeoutValuePair(value, timeout);
  +        }
  +        put(txId, key, value, byteSize);
  +    }
  +    
       public synchronized void put(Object txId, Object key, Object value, long 
byteSize) {
           if (key == null) {
               logger.log(txId + " adding null key with byte size " + byteSize, 
logChannel, Logger.WARNING);
  @@ -150,7 +158,7 @@
                   Map.Entry entry = (Map.Entry) it.next();
                   long byteSize = changeCache.getByteSize(entry.getKey());
                   if (byteSize == -1L) {
  -                    globalCache.put(entry.getKey(), entry.getValue());
  +                    putToGlobalCache(entry.getKey(), entry.getValue());
                   } else {
                       ((SizeCountingLRUMap) globalCache).put(entry.getKey(), 
entry.getValue(), byteSize);
                   }
  
  
  
  1.20      +39 -6     
jakarta-slide/src/share/org/apache/slide/store/ExtendedStore.java
  
  Index: ExtendedStore.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/store/ExtendedStore.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- ExtendedStore.java        19 Oct 2004 11:47:05 -0000      1.19
  +++ ExtendedStore.java        20 Oct 2004 07:22:44 -0000      1.20
  @@ -113,6 +113,9 @@
       protected static final String CACHE_MODE_OFF = "off";
       protected static final String DEFAULT_CACHE_MODE = CACHE_MODE_FULL;
   
  +    protected static final int DEFAULT_CACHE_TIMEOUT = -1; // infinite
  +    protected static final String CACHE_TIMEOUT_PARAMETER = "cache-timeout";
  +
       protected static final int DEFAULT_TLOCK_TIMEOUT = 20000; // 20 sec
       protected static final String TLOCK_TIMEOUT_PARAMETER = "tlock-timeout";
   
  @@ -129,6 +132,8 @@
       protected GenericLockManager lockManager;
       protected ThreadLocal locks = new ThreadLocal();
       protected long timeout;
  +    protected long cacheTimeout;
  +    
   
       protected boolean globalCacheOff;
   
  @@ -167,6 +172,22 @@
           txContentCacheBytes = DEFAULT_TX_CONTENT_CACHE_BYTES;
           maxByteSizePerEntry = DEFAULT_MAX_CONTENT_BYTES_PER_ENTRY;
   
  +        cacheTimeout = DEFAULT_CACHE_TIMEOUT;
  +        String cacheTimeoutString = (String) 
parameters.get(CACHE_TIMEOUT_PARAMETER);
  +        if (cacheTimeoutString != null) {
  +            try {
  +                cacheTimeout = Long.parseLong(cacheTimeoutString) * 1000;
  +            } catch (NumberFormatException nfe) {
  +                getLogger().log("Cache timeout must be integer! Using default 
value", LOG_CHANNEL, Logger.WARNING);
  +            }
  +        }
  +        if (cacheTimeout != -1) {
  +            getLogger().log("Setting cache timeout for store " + getName() + " to " 
+ cacheTimeout / 1000 + " seconds",
  +                    Logger.INFO);
  +        } else {
  +            getLogger().log("Disabling cache timeout for store " + getName(), 
Logger.INFO);
  +        }
  +
           timeout = DEFAULT_TLOCK_TIMEOUT;
           String timeoutString = (String) parameters.get(TLOCK_TIMEOUT_PARAMETER);
           if (timeoutString != null) {
  @@ -1208,11 +1229,15 @@
           }
   
           public void put(Object key, Object value) {
  +            put(key, value, ExtendedStore.this.cacheTimeout);
  +        }
  +        
  +        public void put(Object key, Object value, long timeout) {
               if (globalCacheOff)
                   return;
               try {
                   Xid txId = (Xid) activeTransactionBranch.get();
  -                txCache.put(txId, key, value);
  +                txCache.put(txId, key, value, timeout);
               } catch (Error e) {
                   fatalError(e);
               } catch (RuntimeException re) {
  @@ -1296,6 +1321,10 @@
           }
   
           public void putForRead(Object key, NodeRevisionContent revisionContent, 
long byteSize) {
  +            putForRead(key, revisionContent, byteSize, 
ExtendedStore.this.cacheTimeout);
  +        }
  +        
  +        public void putForRead(Object key, NodeRevisionContent revisionContent, 
long byteSize, long timeout) {
               if (globalCacheOff)
                   return;
               try {
  @@ -1319,6 +1348,10 @@
           }
   
           public void put(Object key, NodeRevisionContent revisionContent, long 
byteSize) {
  +            put(key, revisionContent, byteSize, ExtendedStore.this.cacheTimeout);
  +        }
  +        
  +        public void put(Object key, NodeRevisionContent revisionContent, long 
byteSize, long timeout) {
               if (globalCacheOff)
                   return;
               try {
  @@ -1328,7 +1361,7 @@
                       // buffer stream content into byte array for multiple access
                       revisionContent.getContentBytes();
   
  -                    ((ByteSizeLimitedObjectCache) getTxCache()).put(txId, key, 
revisionContent, byteSize);
  +                    ((ByteSizeLimitedObjectCache) getTxCache()).put(txId, key, 
revisionContent, byteSize, timeout);
                       getLogger().log(
                           "Caching content at '" + key + "' with " + byteSize + " 
bytes",
                           LOG_CHANNEL,
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to