Index: src/java/org/apache/solr/common/util/ConcurrentLRUCache.java
===================================================================
--- src/java/org/apache/solr/common/util/ConcurrentLRUCache.java	(revision 0)
+++ src/java/org/apache/solr/common/util/ConcurrentLRUCache.java	(revision 0)
@@ -0,0 +1,263 @@
+package org.apache.solr.common.util;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * @author Noble Paul (Noble.Paul@corp.aol.com)
+ *         Date: Jul 29, 2008
+ *         Time: 12:53:53 PM
+ */
+public class ConcurrentLRUCache {
+
+  private Map<Object, Entry> map;
+  private int upperWaterMark, lowerWaterMark;
+  private boolean stop = false;
+  private Lock markAndSweepLock = new ReentrantLock(true);
+  private boolean newThreadForCleanup = false;
+  private volatile boolean islive = true;
+  private final Stats stats = new Stats();
+
+  public ConcurrentLRUCache(int upperWaterMark, final int lowerWaterMark, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, final int delay) {
+    if (upperWaterMark < 1) throw new IllegalArgumentException("upperWaterMark must be > 0");
+    if (lowerWaterMark >= upperWaterMark)
+      throw new IllegalArgumentException("lowerWaterMark must be  < upperWaterMark");
+    map = new ConcurrentHashMap<Object, Entry>(initialSize);
+    newThreadForCleanup = runNewThreadForCleanup;
+    this.upperWaterMark = upperWaterMark;
+    this.lowerWaterMark = lowerWaterMark;
+    if (runCleanupThread) {
+      new Thread() {
+        public void run() {
+          while (true) {
+            if (stop) break;
+            try {
+              Thread.sleep(delay * 1000);
+            } catch (InterruptedException e) {/*no op*/ }
+            markAndSweep();
+          }
+        }
+      }.start();
+    }
+  }
+
+  public void setAlive(boolean live) {
+    islive = live;
+  }
+
+  public Object get(Object key) {
+    Entry e = map.get(key);
+    if (e == null) {
+      if (islive) stats.missCounter.incrementAndGet();
+      return null;
+    }
+    if (islive) e.setLastAccessed(stats.accessCounter.incrementAndGet());
+    return e.value;
+  }
+
+  public void remove(Object key) {
+    map.remove(key);
+  }
+
+  public Object put(Object key, Object val) {
+    if (val == null) return null;
+    Entry e = new Entry(key, val, stats.accessCounter.incrementAndGet());
+    Entry oldEntry = map.put(key, e);
+    if (islive) {
+      stats.putCounter.incrementAndGet();
+    } else {
+      stats.nonLivePutCounter.incrementAndGet();
+    }
+    if (map.size() > upperWaterMark) {
+      if (newThreadForCleanup) {
+        if (markAndSweepLock.tryLock()) {
+          new Thread() {
+            public void run() {
+              markAndSweep();
+            }
+          }.start();
+        }
+      } else {
+        markAndSweep();
+      }
+    }
+    return oldEntry == null ? null : oldEntry.value;
+  }
+
+  private void markAndSweep() {
+    if (!markAndSweepLock.tryLock()) return;
+    markAndSweepLock.lock();
+    try {
+      System.out.println("ConcurrentLRUCache.markAndSweep");
+      int itemsToBeremoved = map.size() - lowerWaterMark;
+      if (itemsToBeremoved < 1) return;
+      TreeSet<SortEntry> tree = new TreeSet<SortEntry>();
+      for (Map.Entry<Object, Entry> entry : map.entrySet()) {
+        if (tree.size() < itemsToBeremoved) {
+          tree.add(new SortEntry(entry.getValue()));
+        } else {
+          long lastAccessed = entry.getValue().lastAccessed;
+          if (lastAccessed < tree.first().lastAccessed) {
+            tree.remove(tree.first());
+            tree.add(new SortEntry(entry.getValue()));
+          }
+        }
+      }
+      stats.evictionCounter += tree.size();
+      for (SortEntry sortEntry : tree) map.remove(sortEntry.entry.key);
+    } finally {
+      markAndSweepLock.unlock();
+    }
+  }
+
+  public Map getLatestAccessedItems(long n) {
+    Map result = new LinkedHashMap();
+    markAndSweepLock.lock();
+    try {
+      TreeSet<SortEntry> tree = new TreeSet<SortEntry>();
+      for (Map.Entry<Object, Entry> entry : map.entrySet()) {
+        if (tree.size() < n) {
+          tree.add(new SortEntry(entry.getValue()));
+        } else {
+          long lastAccessed = entry.getValue().lastAccessed;
+          if (lastAccessed > tree.last().lastAccessed) {
+            tree.remove(tree.last());
+            tree.add(new SortEntry(entry.getValue()));
+          }
+        }
+      }
+      for (SortEntry e : tree) {
+        result.put(e.entry.key, e.entry.value);
+      }
+    } finally {
+      markAndSweepLock.unlock();
+    }
+    return result;
+  }
+
+  public int size() {
+    return map.size();
+  }
+
+  public void clear() {
+    map.clear();
+
+  }
+
+  public Map<Object, Entry> getMap() {
+    return map;
+  }
+
+  private static class SortEntry implements Comparable<SortEntry> {
+    Entry entry;
+    final long lastAccessed;
+
+    public SortEntry(Entry e) {
+      this.entry = e;
+      lastAccessed = e.lastAccessed;
+    }
+
+    public int compareTo(SortEntry that) {
+      if (this.lastAccessed == that.lastAccessed) return 0;
+      return this.lastAccessed < that.lastAccessed ? 1 : -1;
+    }
+
+    public boolean equals(Object obj) {
+      if (obj instanceof SortEntry) {
+        SortEntry sortEntry = (SortEntry) obj;
+        return entry.equals(sortEntry.entry);
+      }
+      return false;
+    }
+
+    public String toString() {
+      return entry.toString();
+    }
+  }
+
+  private static class Entry {
+    Object key, value;
+    volatile long lastAccessed = 0;
+
+
+    public Entry(Object key, Object value, long lastAccessed) {
+      this.key = key;
+      this.value = value;
+      this.lastAccessed = lastAccessed;
+    }
+
+    public synchronized void setLastAccessed(long lastAccessed) {
+      this.lastAccessed = lastAccessed;
+    }
+
+    public int hashCode() {
+      return value.hashCode();
+    }
+
+    public boolean equals(Object obj) {
+      return value.equals(obj);
+    }
+
+    public String toString() {
+      return "key: " + key + " value: " + value + " lastAccessed:" + lastAccessed;
+    }
+  }
+
+
+  public void destroy() {
+    stop = true;
+    if (map != null) {
+      map.clear();
+      map = null;
+    }
+
+  }
+
+  public Stats getStats() {
+    return stats;
+  }
+
+  protected void finalize() throws Throwable {
+    destroy();
+  }
+
+  public static class Stats {
+    public final AtomicLong accessCounter = new AtomicLong(0),
+            putCounter = new AtomicLong(0),
+            nonLivePutCounter = new AtomicLong(0),
+            missCounter = new AtomicLong();
+    private long evictionCounter = 0;
+
+    public long getCumulativeLookups() {
+      return (accessCounter.get() - putCounter.get() - nonLivePutCounter.get()) + missCounter.get();
+    }
+
+    public long getCumulativeHits() {
+      return accessCounter.get() - putCounter.get() - nonLivePutCounter.get();
+    }
+
+    public long getCumulativePuts() {
+      return putCounter.get();
+    }
+
+    public long getCumulativeEvictions() {
+      return evictionCounter;
+    }
+  }
+
+  public static void main(String[] args) {
+    ConcurrentLRUCache clc = new ConcurrentLRUCache(100, 90, 90, false, false, 0);
+    for (int i = 0; i < 25; i++) {
+      clc.put(i, "" + i);
+    }
+    Map m = clc.getLatestAccessedItems(10);
+    System.out.println(m);
+  }
+
+
+}
Index: src/java/org/apache/solr/search/FastLRUCache.java
===================================================================
--- src/java/org/apache/solr/search/FastLRUCache.java	(revision 0)
+++ src/java/org/apache/solr/search/FastLRUCache.java	(revision 0)
@@ -0,0 +1,221 @@
+package org.apache.solr.search;
+
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.common.util.ConcurrentLRUCache;
+//import org.apache.solr.core.SolrCore;
+
+import java.util.*;
+import java.io.IOException;
+import java.io.Serializable;
+import java.net.URL;
+
+/**
+ * @author Noble Paul (Noble.Paul@corp.aol.com)
+ *         Date: Aug 12, 2008
+ *         Time: 3:17:26 PM
+ */
+public class FastLRUCache implements SolrCache {
+
+  private List<ConcurrentLRUCache.Stats> cumulativeStats;
+
+  private long warmupTime = 0;
+
+  private String name;
+  private int autowarmCount;
+  private State state;
+  private CacheRegenerator regenerator;
+  private String description="Concurrent LRU Cache";
+  private ConcurrentLRUCache cache;
+
+  public Object init(Map args, Object persistence, CacheRegenerator regenerator) {
+    state=State.CREATED;
+    this.regenerator = regenerator;
+    name = (String)args.get("name");
+    String str = (String)args.get("size");
+    final int limit = str==null ? 1024 : Integer.parseInt(str);
+    final int minLimit = (int) (limit * 0.9);
+    str = (String)args.get("initialSize");
+    final int initialSize = Math.min(str==null ? 1024 : Integer.parseInt(str), limit);
+    str = (String)args.get("autowarmCount");
+    autowarmCount = str==null ? 0 : Integer.parseInt(str);
+
+    description = "Concurrent LRU Cache(maxSize=" + limit + ", initialSize=" + initialSize;
+    if (autowarmCount>0) {
+      description += ", autowarmCount=" + autowarmCount
+              + ", regenerator=" + regenerator;
+    }
+    description += ')';
+
+    cache = new ConcurrentLRUCache(limit, minLimit,initialSize, false,false, -1);
+    cache.setAlive(false);
+
+    if (persistence==null) {
+      // must be the first time a cache of this type is being created
+      persistence = new ArrayList<ConcurrentLRUCache.Stats>();
+    }
+    cumulativeStats = (List<ConcurrentLRUCache.Stats>)persistence;
+
+    cumulativeStats.add(cache.getStats());
+
+    return cumulativeStats;
+  }
+
+  public String name() {
+    return name;
+  }
+
+  public int size() {
+    return cache.size();
+
+  }
+
+  public Object put(Object key, Object value) {
+    return cache.put(key, value);
+  }
+
+  public Object get(Object key) {
+    return cache.get(key);
+
+  }
+
+  public void clear() {
+    cache.clear();
+  }
+
+  public void setState(State state) {
+    this.state = state;
+    cache.setAlive(state == State.LIVE);
+  }
+
+  public State getState() {
+    return state;
+  }
+
+  public void warm(SolrIndexSearcher searcher, SolrCache old) throws IOException {
+    if (regenerator == null) return;
+    long warmingStartTime = System.currentTimeMillis();
+    FastLRUCache other = (FastLRUCache) old;
+    // warm entries
+    if (autowarmCount != 0) {
+      int sz = other.size();
+      if (autowarmCount != -1) sz = Math.min(sz, autowarmCount);
+      Map items = other.cache.getLatestAccessedItems(sz);
+      Map.Entry[] itemsArr = new Map.Entry[items.size()];
+      int counter = 0;
+      for (Object mapEntry : items.entrySet()) itemsArr[counter++] = (Map.Entry) mapEntry;
+      for (int i = itemsArr.length - 1; i >= 0; i--) {
+        try {
+          boolean continueRegen = regenerator.regenerateItem(searcher,
+                  this, old, itemsArr[i].getKey(), itemsArr[i].getValue());
+          if (!continueRegen) break;
+        }
+        catch (Throwable e) {
+          SolrException.log(log, "Error during auto-warming of key:" + itemsArr[i].getKey(), e);
+        }
+      }
+    }
+    warmupTime = System.currentTimeMillis() - warmingStartTime;
+  }
+
+
+  public void close() {
+  }
+
+
+  //////////////////////// SolrInfoMBeans methods //////////////////////
+
+
+  public String getName() {
+    return FastLRUCache.class.getName();
+  }
+
+  public String getVersion() {
+    return "SolrCore.version";
+  }
+
+  public String getDescription() {
+    return description;
+  }
+
+  public Category getCategory() {
+    return Category.CACHE;
+  }
+
+  public String getSourceId() {
+    return "$Id$";
+  }
+
+  public String getSource() {
+    return "$URL$";
+  }
+
+  public URL[] getDocs() {
+    return null;
+  }
+
+
+  // returns a ratio, not a percent.
+  private static String calcHitRatio(long lookups, long hits) {
+    if (lookups==0) return "0.00";
+    if (lookups==hits) return "1.00";
+    int hundredths = (int)(hits*100/lookups);   // rounded down
+    if (hundredths < 10) return "0.0" + hundredths;
+    return "0." + hundredths;
+
+    /*** code to produce a percent, if we want it...
+    int ones = (int)(hits*100 / lookups);
+    int tenths = (int)(hits*1000 / lookups) - ones*10;
+    return Integer.toString(ones) + '.' + tenths;
+    ***/
+  }
+
+  public NamedList getStatistics() {
+    NamedList<Serializable> lst = new SimpleOrderedMap<Serializable>();
+    ConcurrentLRUCache.Stats stats = cache.getStats();
+    long lookups = stats.getCumulativeLookups();
+    long hits = stats.getCumulativeHits();
+    long inserts = stats.getCumulativePuts();
+    long evictions = stats.getCumulativeEvictions();
+    long size = cache.size();
+
+    lst.add("lookups", lookups);
+    lst.add("hits", hits);
+    lst.add("hitratio", calcHitRatio(lookups,hits));
+    lst.add("inserts", inserts);
+    lst.add("evictions", evictions);
+    lst.add("size", size);
+
+    lst.add("warmupTime", warmupTime);
+
+
+    long clookups = 0;
+    long chits = 0;
+    long cinserts = 0;
+    long cevictions = 0;
+    for (ConcurrentLRUCache.Stats statistiscs : cumulativeStats) {
+      clookups += statistiscs.getCumulativeLookups();
+      chits += statistiscs.getCumulativeHits();
+      cinserts += statistiscs.getCumulativePuts();
+      cevictions += statistiscs.getCumulativeEvictions();
+    }
+    lst.add("cumulative_lookups", clookups);
+    lst.add("cumulative_hits", chits);
+    lst.add("cumulative_hitratio", calcHitRatio(clookups,chits));
+    lst.add("cumulative_inserts", cinserts);
+    lst.add("cumulative_evictions", cevictions);
+
+    return lst;
+  }
+
+  public String toString() {
+    return name + getStatistics().toString();
+  }
+
+  public static void main(String[] args) {
+    
+
+  }
+}
+
Index: src/test/org/apache/solr/search/TestFastLRUCache.java
===================================================================
--- src/test/org/apache/solr/search/TestFastLRUCache.java	(revision 0)
+++ src/test/org/apache/solr/search/TestFastLRUCache.java	(revision 0)
@@ -0,0 +1,63 @@
+package org.apache.solr.search;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.io.IOException;
+
+import org.apache.solr.common.util.NamedList;
+
+
+/**
+ * @author Noble Paul (Noble.Paul@corp.aol.com)
+ *         Date: Aug 18, 2008
+ *         Time: 3:38:14 PM
+ */
+public class TestFastLRUCache extends TestCase {
+  public void testSimple() throws IOException {
+    FastLRUCache sc = new FastLRUCache();
+    Map l = new HashMap();
+    l.put("size","100");
+    l.put("initialSize","10");
+    l.put("autowarmCount","25");
+    CacheRegenerator cr = new CacheRegenerator() {
+      public boolean regenerateItem(SolrIndexSearcher newSearcher, SolrCache newCache,
+                                    SolrCache oldCache, Object oldKey, Object oldVal) throws IOException {
+        newCache.put(oldKey, oldVal);
+        return true;
+      }
+    };
+    Object o =  sc.init(l,null,cr);
+    sc.setState(SolrCache.State.LIVE);
+    for(int i=0;i<101;i++){
+      sc.put(i+1,""+(i+1));
+    }
+    assertEquals("25",sc.get(25));
+    assertEquals(null,sc.get(110));
+    NamedList nl = sc.getStatistics();
+    assertEquals(2L ,nl.get("lookups"));
+    assertEquals(1L ,nl.get("hits"));
+    assertEquals(101L ,nl.get("inserts"));
+    assertEquals(11L ,nl.get("evictions"));
+
+    FastLRUCache scNew = new FastLRUCache();
+    scNew.init(l,o,cr);
+    scNew.warm(null,sc);
+    scNew.setState(SolrCache.State.LIVE);
+    scNew.put(103,"103");
+    assertEquals("90",scNew.get(90));
+    assertEquals(null,scNew.get(50));
+    nl = scNew.getStatistics();
+    assertEquals(2L ,nl.get("lookups"));
+    assertEquals(1L ,nl.get("hits"));
+    assertEquals(1L ,nl.get("inserts"));
+    assertEquals(0L ,nl.get("evictions"));
+
+    assertEquals(4L ,nl.get("cumulative_lookups"));
+    assertEquals(2L ,nl.get("cumulative_hits"));
+    assertEquals(102L ,nl.get("cumulative_inserts"));
+    assertEquals(11L ,nl.get("cumulative_evictions"));
+  }
+
+}
