001package io.ebean.cache;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005import java.util.Set;
006
007/**
008 * Represents part of the "L2" server side cache.
009 * <p>
010 * This is used to cache beans or query results (bean collections).
011 * </p>
012 * <p>
013 * There are 2 ServerCache's for each bean type. One is used as the 'bean cache'
014 * which holds beans of a given type. The other is the 'query cache' holding
015 * query results for a given type.
016 * </p>
017 */
018public interface ServerCache {
019
020  default Map<Object, Object> getAll(Set<Object> keys) {
021
022    Map<Object, Object> map = new LinkedHashMap<>();
023    for (Object key : keys) {
024      Object value = get(key);
025      if (value != null) {
026        map.put(key, value);
027      }
028    }
029    return map;
030  }
031
032  /**
033   * Return the value given the key.
034   */
035  Object get(Object id);
036
037  /**
038   * Put all the values in the cache.
039   */
040  default void putAll(Map<Object, Object> keyValues) {
041    keyValues.forEach(this::put);
042  }
043
044  /**
045   * Put the value in the cache with a given id.
046   */
047  void put(Object id, Object value);
048
049  /**
050   * Remove the entries from the cache given the id values.
051   */
052  default void removeAll(Set<Object> keys) {
053    keys.forEach(this::remove);
054  }
055
056  /**
057   * Remove a entry from the cache given its id.
058   */
059  void remove(Object id);
060
061  /**
062   * Clear all entries from the cache.
063   */
064  void clear();
065
066  /**
067   * Return the number of entries in the cache.
068   */
069  int size();
070
071  /**
072   * Return the hit ratio the cache is currently getting.
073   */
074  int getHitRatio();
075
076  /**
077   * Return statistics for the cache.
078   *
079   * @param reset if true the statistics are reset.
080   */
081  ServerCacheStatistics getStatistics(boolean reset);
082}