001package io.ebean;
002
003/**
004 * Enum to control the different cache modes for queryCache and beanCache.
005 * <h3>Bean cache</h3>
006 * <p>
007 * The bean cache is automatically used by default on <code>@Cache</code> beans for
008 * the following queries:
009 * </p>
010 * <ul>
011 * <li>findOne() by id</li>
012 * <li>findOne() by natural key(s)</li>
013 * <li>findList() by ids</li>
014 * </ul>
015 * <p>
016 * Bean caching needs to be explicitly turned on for queries that are findList() by natural keys.
017 * </p>
018 * <h3>Query cache</h3>
019 * <p>
020 * For query cache use note that you must be careful, what you do with the returned collection.
021 * By default the returned collections are read only and you will get an exception if you try
022 * to change them.
023 * If you add ".setReadOnly(false)" to your query, you'll get a collection that is a clone from the
024 * one in the cache. That means, changing does not affect the cache.
025 * </p>
026 *
027 * @author Roland Praml, FOCONIS AG
028 */
029public enum CacheMode {
030
031  /**
032   * Do not use cache.
033   */
034  OFF(false, false),
035
036  /**
037   * Use the cache and store a result when needed.
038   */
039  ON(true, true),
040
041  /**
042   * Only used for bean caching.
043   * <p>
044   * The bean cache is automatically used by default on <code>@Cache</code> beans for
045   * the following queries:
046   * </p>
047   * <ul>
048   * <li>findOne() by id</li>
049   * <li>findOne() by natural key(s)</li>
050   * <li>findList() by ids</li>
051   * </ul>
052   * <p>
053   * Bean caching needs to be explicitly turned on for queries that are findList() by natural keys.
054   * </p>
055   */
056  AUTO(true, true),
057
058  /**
059   * Do not read from cache, but put beans into the cache and invalidate parts of the cache as necessary.
060   * <p>
061   * Use this on a query if you want to get the fresh value from database and put it into the cache.
062   */
063  PUT(false, true),
064
065  /**
066   * GET only from the cache.
067   * <p>
068   * This mode does not put entries into the cache or invalidate parts of the cache.
069   */
070  GET(true, false);
071
072  private final boolean get;
073  private final boolean put;
074
075  CacheMode(boolean get, boolean put) {
076    this.get = get;
077    this.put = put;
078  }
079
080  /**
081   * Return true if value is read from cache.
082   */
083  public boolean isGet() {
084    return get;
085  }
086
087  /**
088   * Return true if a newly loaded value (from database) is put into the cache.
089   */
090  public boolean isPut() {
091    return put;
092  }
093}