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