001package com.avaje.ebean.cache;
002
003import com.avaje.ebean.EbeanServer;
004
005/**
006 * Represents part of the "L2" server side cache.
007 * <p>
008 * This is used to cache beans or query results (bean collections).
009 * </p>
010 * <p>
011 * There are 2 ServerCache's for each bean type. One is used as the 'bean cache'
012 * which holds beans of a given type. The other is the 'query cache' holding
013 * query results for a given type.
014 * </p>
015 * 
016 * @author rbygrave
017 */
018public interface ServerCache {
019
020  /**
021   * Just after a cache is created this init method is called. This is so that a
022   * cache implementation can make use of the BackgroundExecutor service to
023   * trim/cleanup itself or use the EbeanServer to populate itself.
024   * <p>
025   * This method is called after the cache is constructed but before the cache
026   * is made available for use.
027   * </p>
028   */
029  void init(EbeanServer ebeanServer);
030
031  /**
032   * Return the configuration options for this cache.
033   */
034  ServerCacheOptions getOptions();
035
036  /**
037   * Update the configuration options for this cache.
038   */
039  void setOptions(ServerCacheOptions options);
040
041  /**
042   * Return the value given the key.
043   */
044  Object get(Object id);
045
046  /**
047   * Put the value in the cache with a given id.
048   */
049  Object put(Object id, Object value);
050
051  /**
052   * Remove a entry from the cache given its id.
053   */
054  Object remove(Object id);
055
056  /**
057   * Clear all entries from the cache.
058   * <p>
059   * NOTE: Be careful using this method in that most of the time application
060   * code should clear BOTH the bean and query caches at the same time. This can
061   * be done via {@link ServerCacheManager#clear(Class)}.
062   * </p>
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
080   *          if true the statistics are reset.
081   */
082  ServerCacheStatistics getStatistics(boolean reset);
083}