001package io.ebean.cache;
002
003import io.ebean.annotation.CacheBeanTuning;
004import io.ebean.annotation.CacheQueryTuning;
005
006/**
007 * Options for controlling a cache.
008 */
009public class ServerCacheOptions {
010
011  private int maxSize;
012  private int maxIdleSecs;
013  private int maxSecsToLive;
014  private int trimFrequency;
015  private boolean nearCache;
016
017  /**
018   * Construct with no set options.
019   */
020  public ServerCacheOptions() {
021  }
022
023  /**
024   * Create from the cacheTuning deployment annotation.
025   */
026  public ServerCacheOptions(CacheBeanTuning tuning) {
027    this.maxSize = tuning.maxSize();
028    this.maxIdleSecs = tuning.maxIdleSecs();
029    this.maxSecsToLive = tuning.maxSecsToLive();
030    this.trimFrequency = tuning.trimFrequency();
031  }
032
033  /**
034   * Create from the cacheTuning deployment annotation.
035   */
036  public ServerCacheOptions(CacheQueryTuning cacheTuning) {
037    this.maxSize = cacheTuning.maxSize();
038    this.maxIdleSecs = cacheTuning.maxIdleSecs();
039    this.maxSecsToLive = cacheTuning.maxSecsToLive();
040    this.trimFrequency = cacheTuning.trimFrequency();
041  }
042
043  /**
044   * Create with nearCache option.
045   */
046  public ServerCacheOptions(boolean nearCache, CacheBeanTuning tuning) {
047    this(tuning);
048    this.nearCache = nearCache;
049  }
050
051  /**
052   * Apply any settings from the default settings that have not already been
053   * specifically set.
054   */
055  public ServerCacheOptions applyDefaults(ServerCacheOptions defaults) {
056    if (maxSize == 0) {
057      maxSize = defaults.getMaxSize();
058    }
059    if (maxIdleSecs == 0) {
060      maxIdleSecs = defaults.getMaxIdleSecs();
061    }
062    if (maxSecsToLive == 0) {
063      maxSecsToLive = defaults.getMaxSecsToLive();
064    }
065    if (trimFrequency == 0) {
066      trimFrequency = defaults.getTrimFrequency();
067    }
068    return this;
069  }
070
071  /**
072   * Return a copy of this object.
073   */
074  public ServerCacheOptions copy() {
075    ServerCacheOptions copy = new ServerCacheOptions();
076    copy.maxSize = maxSize;
077    copy.maxIdleSecs = maxIdleSecs;
078    copy.maxSecsToLive = maxSecsToLive;
079    copy.trimFrequency = trimFrequency;
080    copy.nearCache = this.nearCache;
081    return copy;
082  }
083
084  /**
085   * Return a copy of this object with nearCache option.
086   */
087  public ServerCacheOptions copy(boolean nearCache) {
088    ServerCacheOptions copy = copy();
089    copy.nearCache = nearCache;
090    return copy;
091  }
092
093  /**
094   * Return true if nearCache was explicitly turned on.
095   */
096  public boolean isNearCache() {
097    return nearCache;
098  }
099
100  /**
101   * Turn on nearCache option.
102   */
103  public void setNearCache(boolean nearCache) {
104    this.nearCache = nearCache;
105  }
106
107  /**
108   * Return the maximum cache size.
109   */
110  public int getMaxSize() {
111    return maxSize;
112  }
113
114  /**
115   * Set the maximum cache size.
116   */
117  public void setMaxSize(int maxSize) {
118    this.maxSize = maxSize;
119  }
120
121  /**
122   * Return the maximum idle time.
123   */
124  public int getMaxIdleSecs() {
125    return maxIdleSecs;
126  }
127
128  /**
129   * Set the maximum idle time.
130   */
131  public void setMaxIdleSecs(int maxIdleSecs) {
132    this.maxIdleSecs = maxIdleSecs;
133  }
134
135  /**
136   * Return the maximum time to live.
137   */
138  public int getMaxSecsToLive() {
139    return maxSecsToLive;
140  }
141
142  /**
143   * Set the maximum time to live.
144   */
145  public void setMaxSecsToLive(int maxSecsToLive) {
146    this.maxSecsToLive = maxSecsToLive;
147  }
148
149  /**
150   * Return the trim frequency in seconds.
151   */
152  public int getTrimFrequency() {
153    return trimFrequency;
154  }
155
156  /**
157   * Set the trim frequency in seconds.
158   */
159  public void setTrimFrequency(int trimFrequency) {
160    this.trimFrequency = trimFrequency;
161  }
162}