user3761308
user3761308

Reputation: 791

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?

Upvotes: 0

Views: 233

Answers (2)

AlmasB
AlmasB

Reputation: 3407

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

Upvotes: 3

Jean Logeart
Jean Logeart

Reputation: 53839

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);
       }
     });

Upvotes: 2

Related Questions