001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tcclient.cache;
005:
006: import com.tc.object.bytecode.Manager;
007: import com.tc.properties.TCProperties;
008:
009: /**
010: * Cache configuration. This is a shared object so only one should exist. This config is shared
011: * across all nodes involved in the cache and is immutable.
012: */
013: public class CacheConfig {
014:
015: // Configuration
016: private final String cacheName;
017: private final long maxIdleTimeoutSeconds; // max time cache element can live without access
018: private final long maxTTLSeconds; // max time cache element can live regardless of access
019: private final long invalidatorSleepSeconds; // wait time between eviction cycles
020: private final Expirable callback; // callback on expire()
021:
022: // Derived from config
023: private final long maxIdleTimeoutMillis; // same as above, but in milliseconds
024: private final long maxTTLMillis; // same as above, but in milliseconds
025:
026: // Configuration from ehcache properties
027: private final int concurrency; // number of maps in the cache
028: private final int evictorPoolSize; // number of evictor threads, must be <= concurrency
029: private final boolean globalEvictionEnabled;
030: private final int globalEvictionFrequency; // # of times to run local eviction before doing global
031: private final int numOfChunks; // break global eviction into chunks
032: private final long restMillis; // rest between global eviction of each chunk
033: private final boolean isLoggingEnabled; // CacheDataStore logging
034: private final boolean isEvictorLoggingEnabled;// CacheInvalidationTimer logging
035:
036: public CacheConfig(String cacheName,
037: long maxIdleTimeoutSeconds,
038: long maxTTLSeconds,
039: long invalidatorSleepSeconds,
040: Expirable callback,
041: // Start ehcache-related properties
042: int concurrency, int evictorPoolSize,
043: boolean globalEvictionEnabled, int globalEvictionFrequency,
044: boolean isLoggingEnabled, boolean isEvictorLoggingEnabled,
045: int numOfChunks, long restMillis) {
046:
047: this .cacheName = cacheName;
048: this .maxIdleTimeoutSeconds = maxIdleTimeoutSeconds;
049: this .maxTTLSeconds = maxTTLSeconds;
050: this .invalidatorSleepSeconds = invalidatorSleepSeconds;
051: this .callback = callback;
052:
053: this .maxIdleTimeoutMillis = maxIdleTimeoutSeconds * 1000;
054: this .maxTTLMillis = maxTTLSeconds * 1000;
055:
056: this .concurrency = concurrency;
057: this .evictorPoolSize = evictorPoolSize;
058: this .globalEvictionEnabled = globalEvictionEnabled;
059: this .globalEvictionFrequency = globalEvictionFrequency;
060: this .isLoggingEnabled = isLoggingEnabled;
061: this .isEvictorLoggingEnabled = isEvictorLoggingEnabled;
062: this .numOfChunks = numOfChunks;
063: this .restMillis = restMillis;
064: }
065:
066: /**
067: * Extract ehcache config properties from manager.
068: */
069: public CacheConfig(String cacheName, long maxIdleTimeoutSeconds,
070: long maxTTLSeconds, long invalidatorSleepSeconds,
071: Expirable callback, Manager manager) {
072:
073: this .cacheName = cacheName;
074: this .maxIdleTimeoutSeconds = maxIdleTimeoutSeconds;
075: this .maxTTLSeconds = maxTTLSeconds;
076: this .invalidatorSleepSeconds = invalidatorSleepSeconds;
077: this .callback = callback;
078:
079: this .maxIdleTimeoutMillis = maxIdleTimeoutSeconds * 1000;
080: this .maxTTLMillis = maxTTLSeconds * 1000;
081:
082: // Load ehcache properties
083: TCProperties props = manager.getTCProperites();
084: TCProperties ehcacheProperties = props
085: .getPropertiesFor("ehcache");
086: int concurrencyProp = ehcacheProperties.getInt("concurrency");
087: if (concurrencyProp <= 1) {
088: this .concurrency = 1;
089: this .evictorPoolSize = 1;
090: } else {
091: this .concurrency = concurrencyProp;
092: int evictorPoolSizeProp = ehcacheProperties
093: .getInt("evictor.pool.size");
094: if (evictorPoolSizeProp > this .concurrency) {
095: this .evictorPoolSize = this .concurrency;
096: } else {
097: this .evictorPoolSize = evictorPoolSizeProp;
098: }
099: }
100: this .globalEvictionEnabled = ehcacheProperties
101: .getBoolean("global.eviction.enable");
102: this .globalEvictionFrequency = ehcacheProperties
103: .getInt("global.eviction.frequency");
104:
105: TCProperties loggingProperties = props
106: .getPropertiesFor("ehcache.logging");
107: if (loggingProperties != null) {
108: this .isLoggingEnabled = loggingProperties
109: .getBoolean("enabled");
110: } else {
111: this .isLoggingEnabled = false;
112: }
113:
114: TCProperties ehcacheProperies = props
115: .getPropertiesFor("ehcache.global.eviction");
116: this .numOfChunks = ehcacheProperies.getInt("segments");
117: this .restMillis = ehcacheProperies.getLong("rest.timeMillis");
118:
119: TCProperties loggingProperty = props
120: .getPropertiesFor("ehcache.evictor.logging");
121: if (loggingProperty != null) {
122: this .isEvictorLoggingEnabled = loggingProperty
123: .getBoolean("enabled");
124: } else {
125: this .isEvictorLoggingEnabled = false;
126: }
127: }
128:
129: public int getConcurrency() {
130: return concurrency;
131: }
132:
133: public int getEvictorPoolSize() {
134: return evictorPoolSize;
135: }
136:
137: public boolean isGlobalEvictionEnabled() {
138: return globalEvictionEnabled;
139: }
140:
141: public int getGlobalEvictionFrequency() {
142: return globalEvictionFrequency;
143: }
144:
145: public boolean isLoggingEnabled() {
146: return isLoggingEnabled;
147: }
148:
149: public int getNumOfChunks() {
150: return numOfChunks;
151: }
152:
153: public long getRestMillis() {
154: return restMillis;
155: }
156:
157: public String getCacheName() {
158: return cacheName;
159: }
160:
161: public long getMaxIdleTimeoutSeconds() {
162: return maxIdleTimeoutSeconds;
163: }
164:
165: public long getMaxTTLSeconds() {
166: return maxTTLSeconds;
167: }
168:
169: public long getInvalidatorSleepSeconds() {
170: return invalidatorSleepSeconds;
171: }
172:
173: public Expirable getCallback() {
174: return callback;
175: }
176:
177: public long getMaxIdleTimeoutMillis() {
178: return maxIdleTimeoutMillis;
179: }
180:
181: public long getMaxTTLMillis() {
182: return maxTTLMillis;
183: }
184:
185: public int getStoresPerInvalidator() {
186: return concurrency / evictorPoolSize;
187: }
188:
189: public boolean isEvictorLoggingEnabled() {
190: return isEvictorLoggingEnabled;
191: }
192: }
|