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