This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
/
SampleCacheContainer.java
69 lines (59 loc) · 2.74 KB
/
SampleCacheContainer.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ${groupId};
import org.infinispan.Cache;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import java.io.IOException;
/**
* This sample cache container acts as a factory and a mechanism with which to create and configure an embedded cache
* manager, and to hold this cache manager such that other code can access it.
*
* Variants of this pattern include storing the EmbeddedCacheManager in JNDI or as a bean in JMX, or even using CDI
* or some other dependency injection framework to hold the cache manager (and even cache) references and inject them
* as needed into various bits of application code.
*/
public class SampleCacheContainer {
// ************************************************************************************************************
// This should point to the Infinispan configuration file. Either an absolute path or the name of a config
// file in your classpath could be used. See http://community.jboss.org/wiki/Configuringcache for more details.
// ************************************************************************************************************
// This skeleton project ships with 4 different Infinispan configurations. Uncomment the one most appropriate to you.
private static final String INFINISPAN_CONFIGURATION = "infinispan-local.xml";
// private static final String INFINISPAN_CONFIGURATION = "infinispan-clustered-tcp.xml";
// private static final String INFINISPAN_CONFIGURATION = "infinispan-clustered-udp.xml";
// private static final String INFINISPAN_CONFIGURATION = "infinispan-clustered-ec2.xml";
private static final EmbeddedCacheManager CACHE_MANAGER;
static {
try {
CACHE_MANAGER = new DefaultCacheManager(INFINISPAN_CONFIGURATION);
} catch (IOException e) {
throw new RuntimeException("Unable to configure Infinispan", e);
}
}
/**
* Retrieves the default cache.
* @param <K> type used as keys in this cache
* @param <V> type used as values in this cache
* @return a cache
*/
public static <K, V> Cache<K, V> getCache() {
return CACHE_MANAGER.getCache();
}
/**
* Retrieves a named cache.
* @param cacheName name of cache to retrieve
* @param <K> type used as keys in this cache
* @param <V> type used as values in this cache
* @return a cache
*/
public static <K, V> Cache<K, V> getCache(String cacheName) {
if (cacheName == null) throw new NullPointerException("Cache name cannot be null!");
return CACHE_MANAGER.getCache(cacheName);
}
/**
* Retrieves the embedded cache manager.
* @return a cache manager
*/
public static EmbeddedCacheManager getCacheContainer() {
return CACHE_MANAGER;
}
}