tdraier     2005/11/09 14:32:58 CET

  Modified files:
    core/src/java/org/jahia/services/cache/simple 
                                                  SimpleCacheImpl.java 
  Log:
  a little more thread-safe cache
  
  Revision  Changes    Path
  1.3       +23 -19    
jahia/core/src/java/org/jahia/services/cache/simple/SimpleCacheImpl.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/services/cache/simple/SimpleCacheImpl.java.diff?r1=1.2&r2=1.3&f=h
  
  
  
  Index: SimpleCacheImpl.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/services/cache/simple/SimpleCacheImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SimpleCacheImpl.java      9 Nov 2005 13:30:38 -0000       1.2
  +++ SimpleCacheImpl.java      9 Nov 2005 13:32:57 -0000       1.3
  @@ -44,11 +44,13 @@
               return;
           }
           // Add the entry in the cache
  -        if (!cache.containsKey (key)) {
  -            // let's handle maximum size stuff here
  -            checkCacheSize ();
  +        synchronized(cache) {
  +            if (!cache.containsKey (key)) {
  +                // let's handle maximum size stuff here
  +                checkCacheSize ();
  +            }
  +            cache.put(key, value);
           }
  -        cache.put(key, value);
       }
   
       public int size() {
  @@ -125,23 +127,25 @@
               return;
           }
   
  -        while (cache.size () >= getCacheLimit ()) {
  -            logger.debug ("Cache is at limit(" + getCacheLimit () + "), 
removing least used entry.");
  -
  -            // we base the search for the oldest entry by using it's last
  -            // access date time.
  -            Iterator entryIter = cache.entrySet().iterator();
  -            long minTime = Long.MAX_VALUE;
  -            Object minEntryKey = null;
  -            while (entryIter.hasNext()) {
  -                Map.Entry curEntry = (Map.Entry) entryIter.next();
  -                CacheEntry curCacheEntry = (CacheEntry) curEntry.getValue();
  -                if (curCacheEntry.getLastAccessedTimeMillis() < minTime) {
  -                    minTime = curCacheEntry.getLastAccessedTimeMillis();
  -                    minEntryKey = curEntry.getKey();
  +        synchronized(cache) {
  +            while (cache.size () >= getCacheLimit ()) {
  +                logger.debug ("Cache is at limit(" + getCacheLimit () + "), 
removing least used entry.");
  +
  +                // we base the search for the oldest entry by using it's last
  +                // access date time.
  +                Iterator entryIter = cache.entrySet().iterator();
  +                long minTime = Long.MAX_VALUE;
  +                Object minEntryKey = null;
  +                while (entryIter.hasNext()) {
  +                    Map.Entry curEntry = (Map.Entry) entryIter.next();
  +                    CacheEntry curCacheEntry = (CacheEntry) 
curEntry.getValue();
  +                    if (curCacheEntry.getLastAccessedTimeMillis() < minTime) 
{
  +                        minTime = curCacheEntry.getLastAccessedTimeMillis();
  +                        minEntryKey = curEntry.getKey();
  +                    }
                   }
  +                remove(minEntryKey);
               }
  -            remove(minEntryKey);
           }
       }
   
  

Reply via email to