1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.caches.ignite;
17
18 import java.io.File;
19 import java.util.concurrent.locks.ReadWriteLock;
20
21 import org.apache.ibatis.cache.Cache;
22 import org.apache.ibatis.logging.Log;
23 import org.apache.ibatis.logging.LogFactory;
24 import org.apache.ignite.Ignite;
25 import org.apache.ignite.IgniteCache;
26 import org.apache.ignite.IgniteIllegalStateException;
27 import org.apache.ignite.Ignition;
28 import org.apache.ignite.cache.CachePeekMode;
29 import org.apache.ignite.configuration.CacheConfiguration;
30 import org.springframework.beans.factory.BeanDefinitionStoreException;
31 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
32 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
33 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
34 import org.springframework.core.io.FileSystemResource;
35
36
37
38
39
40
41
42 public final class IgniteCacheAdapter implements Cache {
43
44
45 private static final Log log = LogFactory.getLog(IgniteCacheAdapter.class);
46
47
48 private final String id;
49
50
51
52
53 private final ReadWriteLock readWriteLock = new DummyReadWriteLock();
54
55
56 private static final Ignite ignite;
57
58
59 private final IgniteCache<Object, Object> cache;
60
61
62 private static final String CFG_PATH = "config/default-config.xml";
63
64 static {
65 boolean started = false;
66 try {
67 Ignition.ignite();
68 started = true;
69 } catch (IgniteIllegalStateException e) {
70 log.debug("Using the Ignite instance that has been already started.");
71 log.trace("" + e);
72 }
73 if (started) {
74 ignite = Ignition.ignite();
75 } else {
76 ignite = Ignition.start();
77 }
78 }
79
80
81
82
83
84
85
86 @SuppressWarnings("unchecked")
87 public IgniteCacheAdapter(String id) {
88 if (id == null) {
89 throw new IllegalArgumentException("Cache instances require an ID");
90 }
91
92 CacheConfiguration<Object, Object> cacheCfg = null;
93
94 try {
95 DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
96
97 new XmlBeanDefinitionReader(factory).loadBeanDefinitions(new FileSystemResource(new File(CFG_PATH)));
98
99 cacheCfg = (CacheConfiguration<Object, Object>) factory.getBean("templateCacheCfg");
100
101 cacheCfg.setEvictionPolicy(null);
102 cacheCfg.setCacheLoaderFactory(null);
103 cacheCfg.setCacheWriterFactory(null);
104
105
106 cacheCfg.setName(id);
107 } catch (NoSuchBeanDefinitionException | BeanDefinitionStoreException e) {
108
109 log.warn("Initializing the default cache. Consider properly configuring '" + CFG_PATH + "' instead.");
110 log.trace("" + e);
111
112 cacheCfg = new CacheConfiguration<>(id);
113 }
114
115 cache = ignite.getOrCreateCache(cacheCfg);
116
117 this.id = id;
118 }
119
120 @Override
121 public String getId() {
122 return this.id;
123 }
124
125 @Override
126 public void putObject(Object key, Object value) {
127 cache.put(key, value);
128 }
129
130 @Override
131 public Object getObject(Object key) {
132 return cache.get(key);
133 }
134
135 @Override
136 public Object removeObject(Object key) {
137 return cache.remove(key);
138 }
139
140 @Override
141 public void clear() {
142 cache.clear();
143 }
144
145 @Override
146 public int getSize() {
147 return cache.size(CachePeekMode.PRIMARY);
148 }
149
150 @Override
151 public ReadWriteLock getReadWriteLock() {
152 return readWriteLock;
153 }
154 }