001package org.cache2k;
002
003/*
004 * #%L
005 * cache2k API only package
006 * %%
007 * Copyright (C) 2000 - 2015 headissue GmbH, Munich
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the 
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public 
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.List;
028
029/**
030 * Cache configuration. Adheres to bean standard.
031 *
032 * @author Jens Wilke; created: 2013-06-25
033 */
034public class CacheConfig {
035
036  private String name;
037  private Class<?> keyType = Object.class;
038  private Class<?> valueType = Object.class;
039  private Class<?> entryType;
040  private Class<?> implementation;
041  private int maxSize = 2000;
042  private int maxSizeHighBound = Integer.MAX_VALUE;
043  private int maxSizeLowBound = 0;
044  private int heapEntryCapacity = -1;
045  private boolean backgroundRefresh = false;
046  private long expiryMillis  = 10 * 60 * 1000;
047  private long exceptionExpiryMillis = -1;
048  private boolean keepDataAfterExpired = true;
049  private boolean sharpExpiry = false;
050  private List<Object> moduleConfiguration;
051  private boolean suppressExceptions = true;
052
053  public String getName() {
054    return name;
055  }
056
057  /**
058   * Sets the name of a cache. If a name is not specified the caching framework
059   * provides a name for the cache.
060   *
061   * <p>If a name is specified it must be ensured it is unique within the cache
062   * manager. A unique name may consist of a namespace and a counter, e.g.
063   * "com.company.application.AssetCache-1".
064   *
065   * <p>Allowed characters for a cache name, are URL non-reserved characters,
066   * these are: [A-Z], [a-z], [0-9] and [~-_.-], see RFC3986. The reason for
067   * restricting the characters in names, is that the names may be used to derive
068   * other resource names from it, e.g. for file based storage.
069   *
070   * <p>For brevity within log messages and other displays the cache name may be
071   * shortened if the manager name is included as prefix.
072   */
073  public void setName(String name) {
074    this.name = name;
075  }
076
077  public int getMaxSize() {
078    return maxSize;
079  }
080
081  public void setMaxSize(int maxSize) {
082    this.maxSize = maxSize;
083  }
084
085  public int getMaxSizeHighBound() {
086    return maxSizeHighBound;
087  }
088
089  public void setMaxSizeHighBound(int maxSizeHighBound) {
090    if (maxSize > maxSizeHighBound) {
091      maxSize = maxSizeHighBound;
092    }
093    this.maxSizeHighBound = maxSizeHighBound;
094  }
095
096  public int getMaxSizeLowBound() {
097    return maxSizeLowBound;
098  }
099
100  public void setMaxSizeLowBound(int maxSizeLowBound) {
101    if (maxSize < maxSizeLowBound) {
102      maxSize = maxSizeLowBound;
103    }
104    this.maxSizeLowBound = maxSizeLowBound;
105  }
106
107  public boolean isBackgroundRefresh() {
108    return backgroundRefresh;
109  }
110
111  public void setBackgroundRefresh(boolean backgroundRefresh) {
112    this.backgroundRefresh = backgroundRefresh;
113  }
114
115  public Class<?> getKeyType() {
116    return keyType;
117  }
118
119  public void setKeyType(Class<?> keyType) {
120    if (keyType == null) { throw new NullPointerException("null key type not allowed"); }
121    this.keyType = keyType;
122  }
123
124  public Class<?> getValueType() {
125    return valueType;
126  }
127
128  public Class<?> getEntryType() {
129    return entryType;
130  }
131
132  public void setEntryType(Class<?> entryType) {
133    this.entryType = entryType;
134  }
135
136  public void setValueType(Class<?> valueType) {
137    if (valueType == null) { throw new NullPointerException("null value type not allowed"); }
138    this.valueType = valueType;
139  }
140
141  public boolean isEternal() {
142    return expiryMillis == -1 || expiryMillis == Long.MAX_VALUE;
143  }
144
145  /**
146   * Set cache entry don't expiry by time.
147   */
148  public void setEternal(boolean v) {
149    if (v) {
150      this.expiryMillis = -1;
151    }
152  }
153
154  /**
155   * @depcrecated use {@link #setExpiryMillis}
156   */
157  public void setExpirySeconds(int v) {
158    if (v == -1 || v == Integer.MAX_VALUE) {
159      expiryMillis = -1;
160    }
161    expiryMillis = v * 1000;
162  }
163
164  public int getExpirySeconds() {
165    if (isEternal()) {
166      return -1;
167    }
168    return (int) (expiryMillis / 1000);
169  }
170
171  public long getExpiryMillis() {
172    return expiryMillis;
173  }
174
175  /**
176   * The expiry value of all entries. If an entry specific expiry calculation is
177   * determined this is the maximum expiry time. A value of -1 switches expiry off, that
178   * means entries are kept for an eternal time, a value of 0 switches caching off.
179   */
180  public void setExpiryMillis(long expiryMillis) {
181    this.expiryMillis = expiryMillis;
182  }
183
184  public long getExceptionExpiryMillis() {
185    return exceptionExpiryMillis;
186  }
187
188  /**
189   * @see org.cache2k.CacheBuilder#exceptionExpiryDuration
190   */
191  public void setExceptionExpiryMillis(long v) {
192    exceptionExpiryMillis = v;
193  }
194
195  public boolean isKeepDataAfterExpired() {
196    return keepDataAfterExpired;
197  }
198
199  /**
200   * Expired data is kept in the cache until the entry is evicted by the replacement
201   * algorithm. This consumes memory, but if the data is accessed again the previous
202   * data can be used by the cache source for optimizing, e.g. for a get if-modified-since.
203   *
204   * @see org.cache2k.CacheSourceWithMetaInfo
205   */
206  public void setKeepDataAfterExpired(boolean v) {
207    this.keepDataAfterExpired = v;
208  }
209
210  public boolean isSharpExpiry() {
211    return sharpExpiry;
212  }
213
214  /**
215   * @see CacheBuilder#sharpExpiry(boolean)
216   */
217  public void setSharpExpiry(boolean sharpExpiry) {
218    this.sharpExpiry = sharpExpiry;
219  }
220
221  public boolean isSuppressExceptions() {
222    return suppressExceptions;
223  }
224
225  /**
226   * @see CacheBuilder#suppressExceptions(boolean)
227   */
228  public void setSuppressExceptions(boolean suppressExceptions) {
229    this.suppressExceptions = suppressExceptions;
230  }
231
232  public int getHeapEntryCapacity() {
233    return heapEntryCapacity;
234  }
235
236  /**
237   * Maximum number of entries that the cache keeps in the heap.
238   * Only relevant if a storage modules is defined.
239   */
240  public void setHeapEntryCapacity(int v) {
241    this.heapEntryCapacity = v;
242  }
243
244  public List<Object> getModuleConfiguration() {
245    return moduleConfiguration;
246  }
247
248  public void setModuleConfiguration(List<Object> moduleConfiguration) {
249    this.moduleConfiguration = moduleConfiguration;
250  }
251
252  public Class<?> getImplementation() {
253    return implementation;
254  }
255
256  public void setImplementation(Class<?> cacheImplementation) {
257    this.implementation = cacheImplementation;
258  }
259
260  public List<StorageConfiguration> getStorageModules() {
261    if (moduleConfiguration == null) {
262      return Collections.emptyList();
263    }
264    ArrayList<StorageConfiguration> l = new ArrayList<StorageConfiguration>();
265    for (Object o : moduleConfiguration) {
266      if (o instanceof StorageConfiguration) {
267        l.add((StorageConfiguration) o);
268      }
269    }
270    return l;
271  }
272
273}