Skip to contentPackage: CacheFactory
CacheFactory
Coverage
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.cache;
16:
17: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
18: import cz.cvut.kbss.jopa.sessions.CacheManager;
19: import org.slf4j.Logger;
20: import org.slf4j.LoggerFactory;
21:
22: import java.util.Map;
23: import java.util.Objects;
24:
25: /**
26: * Creates second level cache based on the specified properties.
27: *
28: * @author ledvima1
29: */
30: public abstract class CacheFactory {
31:
32: private static final Logger LOG = LoggerFactory.getLogger(CacheFactory.class);
33:
34: private static final String LRU_CACHE = "lru";
35: private static final String TTL_CACHE = "ttl";
36:
37: private CacheFactory() {
38: throw new AssertionError();
39: }
40:
41: /**
42: * Creates new cache based on the specified properties.
43: *
44: * @param properties Configuration of cache
45: * @return Cache implementation
46: */
47: public static CacheManager createCache(Map<String, String> properties) {
48: Objects.requireNonNull(properties);
49: final String enabledStr = properties.get(JOPAPersistenceProperties.CACHE_ENABLED);
50:• if (enabledStr != null && !Boolean.parseBoolean(enabledStr)) {
51: LOG.debug("Second level cache is disabled.");
52: return new DisabledCacheManager();
53: }
54: return createEnabledCache(properties);
55: }
56:
57: private static CacheManager createEnabledCache(Map<String, String> properties) {
58: final String cacheType = properties.getOrDefault(JOPAPersistenceProperties.CACHE_TYPE, LRU_CACHE)
59: .toLowerCase();
60:• switch (cacheType) {
61: case LRU_CACHE:
62: LOG.debug("Using LRU cache.");
63: return new LruCacheManager(properties);
64: case TTL_CACHE:
65: LOG.debug("Using TTL cache.");
66: return new TtlCacheManager(properties);
67: default:
68: throw new IllegalArgumentException("Invalid second level cache type " + cacheType);
69: }
70: }
71: }