1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.cache.memcached.spy; |
35 |
|
|
36 |
|
import info.magnolia.module.cache.Cache; |
37 |
|
import info.magnolia.module.cache.CacheFactory; |
38 |
|
import info.magnolia.objectfactory.ComponentProvider; |
39 |
|
|
40 |
|
import java.io.IOException; |
41 |
|
import java.util.Arrays; |
42 |
|
import java.util.HashMap; |
43 |
|
import java.util.List; |
44 |
|
import java.util.Map; |
45 |
|
|
46 |
|
import javax.inject.Inject; |
47 |
|
|
48 |
|
import org.slf4j.Logger; |
49 |
|
import org.slf4j.LoggerFactory; |
50 |
|
|
51 |
|
import net.spy.memcached.AddrUtil; |
52 |
|
import net.spy.memcached.MemcachedClient; |
53 |
|
import net.spy.memcached.MemcachedClientIF; |
54 |
|
|
55 |
|
|
56 |
|
@link |
57 |
|
|
|
|
| 44.4% |
Uncovered Elements: 10 (18) |
Complexity: 7 |
Complexity Density: 0.58 |
|
58 |
|
public class MemcachedFactory implements CacheFactory { |
59 |
|
|
60 |
|
private static final Logger log = LoggerFactory.getLogger(MemcachedFactory.class); |
61 |
|
|
62 |
|
private ComponentProvider componentProvider; |
63 |
|
|
64 |
|
private int blockingTimeout = 10000; |
65 |
|
private Class<? extends MemcachedWrapper> cacheWrapperImplementation = MemcachedWrapper.class; |
66 |
|
private Map<String, MemcachedConnectionFactoryBuilder> caches = new HashMap<>(); |
67 |
|
|
68 |
|
protected Map<String, MemcachedWrapper> clients = new HashMap<>(); |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
13 |
@Inject... |
71 |
|
public MemcachedFactory(ComponentProvider componentProvider) { |
72 |
13 |
this.componentProvider = componentProvider; |
73 |
|
} |
74 |
|
|
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
76 |
16 |
@Override... |
77 |
|
public Cache getCache(String name) { |
78 |
16 |
if (clients.containsKey(name)) { |
79 |
4 |
return clients.get(name); |
80 |
|
} else { |
81 |
12 |
return clients.get(CacheFactory.DEFAULT_CACHE_NAME); |
82 |
|
} |
83 |
|
} |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
85 |
0 |
@Override... |
86 |
|
public void start(boolean isRestart) { |
87 |
0 |
for (Map.Entry<String, MemcachedConnectionFactoryBuilder> cacheConfig : caches.entrySet()) { |
88 |
0 |
try { |
89 |
0 |
MemcachedClientIF client = new MemcachedClient(cacheConfig.getValue().build(), AddrUtil.getAddresses(cacheConfig.getValue().getServers())); |
90 |
0 |
clients.put(cacheConfig.getKey(), componentProvider.newInstance(cacheWrapperImplementation, client, blockingTimeout, cacheConfig.getKey())); |
91 |
|
} catch (IOException e) { |
92 |
0 |
log.error("Cannot start memcached client for cache: {}", cacheConfig.getKey(), e); |
93 |
|
} |
94 |
|
} |
95 |
|
} |
96 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
97 |
|
@Override... |
98 |
|
public void stop(boolean isRestart) { |
99 |
|
for (MemcachedWrapper cache : clients.values()) { |
100 |
|
cache.shutdown(); |
101 |
|
} |
102 |
|
} |
103 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
104 |
|
@Override... |
105 |
|
public List<String> getCacheNames() { |
106 |
|
return Arrays.asList(caches.keySet().toArray(new String[]{})); |
107 |
|
} |
108 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
109 |
|
public int getBlockingTimeout() {... |
110 |
|
return blockingTimeout; |
111 |
|
} |
112 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
113 |
|
public void setBlockingTimeout(int blockingTimeout) {... |
114 |
|
this.blockingTimeout = blockingTimeout; |
115 |
|
} |
116 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
117 |
|
public Map<String, MemcachedConnectionFactoryBuilder> getCaches() {... |
118 |
|
return caches; |
119 |
|
} |
120 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
121 |
|
public void setCaches(Map<String, MemcachedConnectionFactoryBuilder> caches) {... |
122 |
|
this.caches = caches; |
123 |
|
} |
124 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
125 |
|
public String getCacheWrapperImplementation() {... |
126 |
|
return cacheWrapperImplementation.getName(); |
127 |
|
} |
128 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
129 |
0 |
public void setCacheWrapperImplementation(String cacheWrapperImplementation) {... |
130 |
0 |
try { |
131 |
0 |
this.cacheWrapperImplementation = (Class<? extends MemcachedWrapper>) Class.forName(cacheWrapperImplementation); |
132 |
|
} catch (ClassNotFoundException | ClassCastException e) { |
133 |
0 |
log.error("Invalid implementation class for memcached wrapper: {}", cacheWrapperImplementation, e); |
134 |
|
} |
135 |
|
} |
136 |
|
} |