1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.esigate.cache;
16
17 import java.util.Properties;
18
19 import org.apache.http.impl.client.cache.CacheConfig;
20 import org.esigate.ConfigurationException;
21 import org.esigate.Parameters;
22
23 public final class CacheConfigHelper {
24
25 private CacheConfigHelper() {
26
27 }
28
29 public static CacheConfig createCacheConfig(Properties properties) {
30
31
32 boolean heuristicCachingEnabled = Parameters.HEURISTIC_CACHING_ENABLED.getValue(properties);
33 float heuristicCoefficient = Parameters.HEURISTIC_COEFFICIENT.getValue(properties);
34 long heuristicDefaultLifetimeSecs = Parameters.HEURISTIC_DEFAULT_LIFETIME_SECS.getValue(properties);
35 int maxCacheEntries = Parameters.MAX_CACHE_ENTRIES.getValue(properties);
36 long maxObjectSize = Parameters.MAX_OBJECT_SIZE.getValue(properties);
37
38
39 int minAsynchronousWorkers = Parameters.MIN_ASYNCHRONOUS_WORKERS.getValue(properties);
40 int maxAsynchronousWorkers = Parameters.MAX_ASYNCHRONOUS_WORKERS.getValue(properties);
41 int asynchronousWorkerIdleLifetimeSecs = Parameters.ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECS.getValue(properties);
42 int maxUpdateRetries = Parameters.MAX_UPDATE_RETRIES.getValue(properties);
43 int revalidationQueueSize = Parameters.REVALIDATION_QUEUE_SIZE.getValue(properties);
44
45 CacheConfig.Builder builder = CacheConfig.custom();
46 builder.setHeuristicCachingEnabled(heuristicCachingEnabled);
47 builder.setHeuristicCoefficient(heuristicCoefficient);
48 builder.setHeuristicDefaultLifetime(heuristicDefaultLifetimeSecs);
49 builder.setMaxCacheEntries(maxCacheEntries);
50 long usedMaxObjectSize = Long.MAX_VALUE;
51 if (maxObjectSize > 0) {
52 usedMaxObjectSize = maxObjectSize;
53 }
54 builder.setMaxObjectSize(usedMaxObjectSize);
55 builder.setAsynchronousWorkersCore(minAsynchronousWorkers);
56 builder.setAsynchronousWorkersMax(maxAsynchronousWorkers);
57 builder.setAsynchronousWorkerIdleLifetimeSecs(asynchronousWorkerIdleLifetimeSecs);
58 builder.setMaxUpdateRetries(maxUpdateRetries).setRevalidationQueueSize(revalidationQueueSize);
59 builder.setSharedCache(true);
60 return builder.build();
61 }
62
63 public static CacheStorage createCacheStorage(Properties properties) {
64 String cacheStorageClass = Parameters.CACHE_STORAGE.getValue(properties);
65 Object cacheStorageObject;
66 try {
67 cacheStorageObject = Class.forName(cacheStorageClass).newInstance();
68 } catch (Exception e) {
69 throw new ConfigurationException("Could not instantiate cacheStorageClass", e);
70 }
71 if (!(cacheStorageObject instanceof CacheStorage)) {
72 throw new ConfigurationException("Cache storage class must extend org.esigate.cache.CacheStorage.");
73 }
74 CacheStorage cacheStorage = (CacheStorage) cacheStorageObject;
75 cacheStorage.init(properties);
76 return cacheStorage;
77 }
78
79 }