Skip to content

Method: createEnabledCache(Map)

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