简体   繁体   中英

Most efficient way to cache last n query results?

I have a simple webapp project where users can enter search terms and results are returned. I thought to cache last n = 10 results in memory (it's FIFO) to optimize it, but don't know the best way to do that.

I thought hashmaps would be the best due to their O(1) search, but (synchronized)Hashmap can't check which was the first added key to be replaced when you want to store the 11th query for example; and LinkedHashmap & Queues don't have good quick .contains() method.

Any good way to buffer last n results in java?

It seems you need a LRU cache, which can easily be implemented on top of LinkedHashMap. Copied from here :

import java.util.LinkedHashMap;
import java.util.Map;

public LRUCache<K, V> extends LinkedHashMap<K, V> {
  private int cacheSize;

  public LRUCache(int cacheSize) {
    super(16, 0.75, true);
    this.cacheSize = cacheSize;
  }

  protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
    return size() >= cacheSize;
  }
}

Simply instantiate it with cacheSize = 10 to fit your use case. As for contains() , LinkedHashMap does it in O(1) .

You can simply use a Guava cache (it is thread safe!):

LoadingCache<Key, Value> cache = CacheBuilder.newBuilder()
  .maximumSize(n)  // n = 10 ?
  .build(
     new CacheLoader<Key, Value>() {
       public Value load(Key key) throws AnyException {
         return getValue(key);
       }
     });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM