1 package com.trendmicro.grid.acl.ds.cache.aspects;
2
3 import com.trendmicro.grid.acl.ds.cache.CacheSettings;
4 import com.trendmicro.grid.acl.ds.cache.ThirdLevelCacheStatisticsSummary;
5 import com.trendmicro.grid.acl.l0.datatypes.Tagged;
6 import net.sf.tinyjee.cache.config.CacheRegion;
7 import net.sf.tinyjee.cache.config.CacheRegions;
8
9 import javax.annotation.Resource;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Map;
13 import java.util.concurrent.Callable;
14
15 import static com.trendmicro.grid.acl.ds.cache.CacheDestination.Mode;
16 import static java.util.concurrent.TimeUnit.HOURS;
17 import static java.util.concurrent.TimeUnit.MINUTES;
18 import static net.sf.tinyjee.cache.config.DistributionPolicy.DISTRIBUTE_VALUES;
19 import static net.sf.tinyjee.cache.config.DistributionPolicy.REPLICATE_VALUES;
20 import static net.sf.tinyjee.cache.config.EvictionPolicy.LIRS;
21 import static net.sf.tinyjee.cache.config.PersistentMode.TEMPORARY;
22
23
24
25
26
27
28
29 @CacheRegions({
30
31 @CacheRegion(name = CacheSettings.CACHE_REGION_FILE_DETAILS,
32
33
34 maxElementsInMemoryExpression = "${max(250, memoryPercent(5) / kb(2))}",
35 persistentMode = TEMPORARY,
36 evictionPolicy = LIRS,
37 distributionPolicy = DISTRIBUTE_VALUES,
38
39 idleTime = 6, idleTimeUnit = HOURS,
40
41 defaultExpirationTimeExpression = "${not runClustered || readonly ? 15 : 6000}", defaultExpirationTimeUnit = MINUTES),
42
43 @CacheRegion(name = CacheSettings.CACHE_REGION_FILE_INFORMATION,
44
45
46 maxElementsInMemoryExpression = "${max(250, memoryPercent(15) / 256)}",
47 persistentMode = TEMPORARY,
48 evictionPolicy = LIRS,
49 distributionPolicy = DISTRIBUTE_VALUES,
50
51 idleTime = 6, idleTimeUnit = HOURS,
52
53 defaultExpirationTimeExpression = "${not runClustered || readonly ? 15 : 6000}", defaultExpirationTimeUnit = MINUTES),
54
55 @CacheRegion(name = CacheSettings.CACHE_REGION_PACKAGE_DETAILS,
56
57
58 maxElementsInMemoryExpression = "${max(250, memoryPercent(3) / kb(4))}",
59 persistentMode = TEMPORARY,
60 evictionPolicy = LIRS,
61 distributionPolicy = DISTRIBUTE_VALUES,
62
63 idleTime = 6, idleTimeUnit = HOURS,
64
65 defaultExpirationTimeExpression = "${not runClustered || readonly ? 15 : 6000}", defaultExpirationTimeUnit = MINUTES),
66
67 @CacheRegion(name = CacheSettings.CACHE_REGION_PACKAGE_INFORMATION,
68
69
70 maxElementsInMemoryExpression = "${max(250, memoryPercent(5) / 512)}",
71 persistentMode = TEMPORARY,
72 evictionPolicy = LIRS,
73 distributionPolicy = DISTRIBUTE_VALUES,
74
75 idleTime = 6, idleTimeUnit = HOURS,
76
77 defaultExpirationTimeExpression = "${not runClustered || readonly ? 15 : 6000}", defaultExpirationTimeUnit = MINUTES),
78
79 @CacheRegion(name = CacheSettings.CACHE_REGION_PACKAGE_FILE_IDENTIFIERS,
80
81
82 maxElementsInMemoryExpression = "${max(250, memoryPercent(2) / 128)}",
83 persistentMode = TEMPORARY,
84 evictionPolicy = LIRS,
85 distributionPolicy = REPLICATE_VALUES,
86
87 idleTime = 6, idleTimeUnit = HOURS,
88
89 defaultExpirationTimeExpression = "${not runClustered || readonly ? 15 : 6000}", defaultExpirationTimeUnit = MINUTES),
90
91 @CacheRegion(name = CacheSettings.CACHE_REGION_UNKNOWN_FILE_IDENTIFIERS,
92
93
94 maxElementsInMemoryExpression = "${max(512, memoryPercent(0.5) / 64)}",
95 persistentMode = TEMPORARY,
96 evictionPolicy = LIRS,
97 distributionPolicy = REPLICATE_VALUES,
98
99 idleTime = 6, idleTimeUnit = HOURS,
100
101 defaultExpirationTimeExpression = "${not runClustered || readonly ? 15 : 120}", defaultExpirationTimeUnit = MINUTES),
102
103 @CacheRegion(name = CacheSettings.CACHE_REGION_UNKNOWN_NAMES,
104
105
106 maxElementsInMemoryExpression = "${max(512, memoryPercent(0.5) / 64)}",
107 persistentMode = TEMPORARY,
108 evictionPolicy = LIRS,
109 distributionPolicy = REPLICATE_VALUES,
110
111 idleTime = 6, idleTimeUnit = HOURS,
112
113 defaultExpirationTimeExpression = "${not runClustered || readonly ? 15 : 120}", defaultExpirationTimeUnit = MINUTES)
114
115 })
116 public abstract class AbstractCacheAspect {
117
118 public static <V> Callable<Collection<V>> chainOf(final Callable<V>... callables) {
119 return new Callable<Collection<V>>() {
120 @Override
121 public Collection<V> call() throws Exception {
122 final Collection<V> results = new ArrayList<V>(callables.length);
123 for (Callable<V> callable : callables) results.add(callable.call());
124
125 return results;
126 }
127 };
128 }
129
130 public static <V> Callable<V> ofFirst(final Callable<V> first, final Callable... runAfters) {
131 return new Callable<V>() {
132 @Override
133 public V call() throws Exception {
134 try {
135 return first.call();
136 } finally {
137 for (Callable runAfter : runAfters) runAfter.call();
138 }
139 }
140 };
141 }
142
143 public static <K, V> Collection<V> call(Callable<Map<K, V>> callable) throws Exception {
144 return callable.call().values();
145 }
146
147
148
149
150
151
152
153
154 protected static Boolean isTaggedWith(Tagged element, String[] tags) {
155 if (element == null)
156 return null;
157 else {
158 for (String tag : tags) {
159 boolean invert = tag.startsWith("-");
160 boolean containsTag = element.containsTag(invert ? tag.substring(1) : tag);
161 if (invert ? containsTag : !containsTag) return false;
162 }
163 return true;
164 }
165 }
166
167 @Resource
168 private ThirdLevelCacheStatisticsSummary cacheStatistics;
169
170 final boolean sync = CacheSettings.IS_SYNCHRONOUS;
171
172 final Mode cacheWriteMode = Mode.Put;
173
174 public ThirdLevelCacheStatisticsSummary getCacheStatistics() {
175 return cacheStatistics;
176 }
177
178 public boolean isSync() {
179 return sync;
180 }
181
182 public Mode getCacheWriteMode() {
183 return cacheWriteMode;
184 }
185 }