Debian Codegraph @

src/org/apache/commons/jcs3/auxiliary/AbstractAuxiliaryCache.java

raw symbols SCIP
1 package org.apache.commons.jcs3.auxiliary;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28
29 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
30 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
31 import org.apache.commons.jcs3.engine.logging.CacheEvent;
32 import org.apache.commons.jcs3.engine.logging.behavior.ICacheEvent;
33 import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
34 import org.apache.commons.jcs3.engine.match.KeyMatcherPatternImpl;
35 import org.apache.commons.jcs3.engine.match.behavior.IKeyMatcher;
36 import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
37
38 /** This holds convenience methods used by most auxiliary caches. */
39 public abstract class AbstractAuxiliaryCache<K, V>
40 implements AuxiliaryCache<K, V>
41 {
42 /** An optional event logger */
43 private ICacheEventLogger cacheEventLogger;
44
45 /** The serializer. Uses a standard serializer by default. */
46 private IElementSerializer elementSerializer = new StandardSerializer();
47
48 /** Key matcher used by the getMatching API */
49 private IKeyMatcher<K> keyMatcher = new KeyMatcherPatternImpl<>();
50
51 /**
52 * Gets multiple items from the cache based on the given set of keys.
53 *
54 * @param keys
55 * @return a map of K key to ICacheElement&lt;K, V&gt; element, or an empty map if there is no
56 * data in cache for any of these keys
57 */
58 protected Map<K, ICacheElement<K, V>> processGetMultiple(final Set<K> keys) throws IOException
59 {
60 if (keys != null)
61 {
62 return keys.stream()
63 .map(key -> {
64 try
65 {
66 return get(key);
67 }
68 catch (final IOException e)
69 {
70 return null;
71 }
72 })
73 .filter(Objects::nonNull)
74 .collect(Collectors.toMap(
75 ICacheElement::getKey,
76 element -> element));
77 }
78
79 return new HashMap<>();
80 }
81
82 /**
83 * Gets the item from the cache.
84 *
85 * @param key
86 * @return ICacheElement, a wrapper around the key, value, and attributes
87 * @throws IOException
88 */
89 @Override
90 public abstract ICacheElement<K, V> get( K key ) throws IOException;
91
92 /**
93 * Logs an event if an event logger is configured.
94 * <p>
95 * @param item
96 * @param eventName
97 * @return ICacheEvent
98 */
99 protected ICacheEvent<K> createICacheEvent( final ICacheElement<K, V> item, final String eventName )
100 {
101 if ( cacheEventLogger == null )
102 {
103 return new CacheEvent<>();
104 }
105 final String diskLocation = getEventLoggingExtraInfo();
106 String regionName = null;
107 K key = null;
108 if ( item != null )
109 {
110 regionName = item.getCacheName();
111 key = item.getKey();
112 }
113 return cacheEventLogger.createICacheEvent( getAuxiliaryCacheAttributes().getName(), regionName, eventName,
114 diskLocation, key );
115 }
116
117 /**
118 * Logs an event if an event logger is configured.
119 * <p>
120 * @param regionName
121 * @param key
122 * @param eventName
123 * @return ICacheEvent
124 */
125 protected <T> ICacheEvent<T> createICacheEvent( final String regionName, final T key, final String eventName )
126 {
127 if ( cacheEventLogger == null )
128 {
129 return new CacheEvent<>();
130 }
131 final String diskLocation = getEventLoggingExtraInfo();
132 return cacheEventLogger.createICacheEvent( getAuxiliaryCacheAttributes().getName(), regionName, eventName,
133 diskLocation, key );
134
135 }
136
137 /**
138 * Logs an event if an event logger is configured.
139 * <p>
140 * @param cacheEvent
141 */
142 protected <T> void logICacheEvent( final ICacheEvent<T> cacheEvent )
143 {
144 if ( cacheEventLogger != null )
145 {
146 cacheEventLogger.logICacheEvent( cacheEvent );
147 }
148 }
149
150 /**
151 * Logs an event if an event logger is configured.
152 * <p>
153 * @param source
154 * @param eventName
155 * @param optionalDetails
156 */
157 protected void logApplicationEvent( final String source, final String eventName, final String optionalDetails )
158 {
159 if ( cacheEventLogger != null )
160 {
161 cacheEventLogger.logApplicationEvent( source, eventName, optionalDetails );
162 }
163 }
164
165 /**
166 * Logs an event if an event logger is configured.
167 * <p>
168 * @param source
169 * @param eventName
170 * @param errorMessage
171 */
172 protected void logError( final String source, final String eventName, final String errorMessage )
173 {
174 if ( cacheEventLogger != null )
175 {
176 cacheEventLogger.logError( source, eventName, errorMessage );
177 }
178 }
179
180 /**
181 * Gets the extra info for the event log.
182 * <p>
183 * @return IP, or disk location, etc.
184 */
185 public abstract String getEventLoggingExtraInfo();
186
187 /**
188 * Allows it to be injected.
189 * <p>
190 * @param cacheEventLogger
191 */
192 @Override
193 public void setCacheEventLogger( final ICacheEventLogger cacheEventLogger )
194 {
195 this.cacheEventLogger = cacheEventLogger;
196 }
197
198 /**
199 * Allows it to be injected.
200 * <p>
201 * @return cacheEventLogger
202 */
203 public ICacheEventLogger getCacheEventLogger()
204 {
205 return this.cacheEventLogger;
206 }
207
208 /**
209 * Allows you to inject a custom serializer. A good example would be a compressing standard
210 * serializer.
211 * <p>
212 * Does not allow you to set it to null.
213 * <p>
214 * @param elementSerializer
215 */
216 @Override
217 public void setElementSerializer( final IElementSerializer elementSerializer )
218 {
219 if ( elementSerializer != null )
220 {
221 this.elementSerializer = elementSerializer;
222 }
223 }
224
225 /**
226 * Allows it to be injected.
227 * <p>
228 * @return elementSerializer
229 */
230 public IElementSerializer getElementSerializer()
231 {
232 return this.elementSerializer;
233 }
234
235 /**
236 * Sets the key matcher used by get matching.
237 * <p>
238 * @param keyMatcher
239 */
240 @Override
241 public void setKeyMatcher( final IKeyMatcher<K> keyMatcher )
242 {
243 if ( keyMatcher != null )
244 {
245 this.keyMatcher = keyMatcher;
246 }
247 }
248
249 /**
250 * Returns the key matcher used by get matching.
251 * <p>
252 * @return keyMatcher
253 */
254 public IKeyMatcher<K> getKeyMatcher()
255 {
256 return this.keyMatcher;
257 }
258 }