Skip to content

Package: CacheFactory

CacheFactory

nameinstructionbranchcomplexitylinemethod
CacheFactory()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createCache(Map)
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
createEnabledCache(Map)
M: 14 C: 23
62%
M: 2 C: 1
33%
M: 2 C: 1
33%
M: 3 C: 4
57%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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