user3761308 user3761308 - 2 months ago 5x
Java Question

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).