CacheService.java
5.97 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Any questions about this component can be directed to it's project Web address
* https://code.google.com/p/opencloudb/.
*
*/
package io.mycat.cache;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
/**
* cache service for other component default using memory cache encache
*
* @author wuzhih
*
*/
public class CacheService {
private static final Logger logger = LoggerFactory.getLogger(CacheService.class);
private final Map<String, CachePoolFactory> poolFactorys = new HashMap<String, CachePoolFactory>();
private final Map<String, CachePool> allPools = new HashMap<String, CachePool>();
public CacheService() {
// load cache pool defined
try {
init();
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
}
}
public Map<String, CachePool> getAllCachePools()
{
return this.allPools;
}
private void init() throws Exception {
Properties props = new Properties();
props.load(CacheService.class
.getResourceAsStream("/cacheservice.properties"));
final String poolFactoryPref = "factory.";
final String poolKeyPref = "pool.";
final String layedPoolKeyPref = "layedpool.";
String[] keys = props.keySet().toArray(new String[0]);
Arrays.sort(keys);
for (String key : keys) {
if (key.startsWith(poolFactoryPref)) {
createPoolFactory(key.substring(poolFactoryPref.length()),
(String) props.get(key));
} else if (key.startsWith(poolKeyPref)) {
String cacheName = key.substring(poolKeyPref.length());
String value = (String) props.get(key);
String[] valueItems = value.split(",");
if (valueItems.length < 3) {
throw new java.lang.IllegalArgumentException(
"invalid cache config ,key:" + key + " value:"
+ value);
}
String type = valueItems[0];
int size = Integer.parseInt(valueItems[1]);
int timeOut = Integer.parseInt(valueItems[2]);
createPool(cacheName, type, size, timeOut);
} else if (key.startsWith(layedPoolKeyPref)) {
String cacheName = key.substring(layedPoolKeyPref.length());
String value = (String) props.get(key);
String[] valueItems = value.split(",");
int index = cacheName.indexOf(".");
if (index < 0) {// root layer
String type = valueItems[0];
int size = Integer.valueOf(valueItems[1]);
int timeOut = Integer.valueOf(valueItems[2]);
createLayeredPool(cacheName, type, size, timeOut);
} else {
// root layers' children
String parent = cacheName.substring(0, index);
String child = cacheName.substring(index + 1);
CachePool pool = this.allPools.get(parent);
if (pool == null || !(pool instanceof LayerCachePool)) {
throw new java.lang.IllegalArgumentException(
"parent pool not exists or not layered cache pool:"
+ parent + " the child cache is:"
+ child);
}
int size = Integer.valueOf(valueItems[0]);
int timeOut = Integer.valueOf(valueItems[1]);
((DefaultLayedCachePool) pool).createChildCache(child,
size, timeOut);
}
}
}
}
private void createLayeredPool(String cacheName, String type, int size,
int expireSeconds) {
checkExists(cacheName);
logger.info("create layer cache pool " + cacheName + " of type " + type
+ " ,default cache size " + size + " ,default expire seconds"
+ expireSeconds);
DefaultLayedCachePool layerdPool = new DefaultLayedCachePool(cacheName,
this.getCacheFact(type), size, expireSeconds);
this.allPools.put(cacheName, layerdPool);
}
private void checkExists(String poolName) {
if (allPools.containsKey(poolName)) {
throw new java.lang.IllegalArgumentException(
"duplicate cache pool name: " + poolName);
}
}
private void createPoolFactory(String factryType, String factryClassName)
throws Exception {
CachePoolFactory factry = (CachePoolFactory) Class.forName(
factryClassName).newInstance();
poolFactorys.put(factryType, factry);
}
private void createPool(String poolName, String type, int cacheSize,
int expireSeconds) {
checkExists(poolName);
CachePoolFactory cacheFact = getCacheFact(type);
CachePool cachePool = cacheFact.createCachePool(poolName, cacheSize,
expireSeconds);
allPools.put(poolName, cachePool);
}
private CachePoolFactory getCacheFact(String type) {
CachePoolFactory facty = this.poolFactorys.get(type);
if (facty == null) {
throw new RuntimeException("CachePoolFactory not defined for type:"
+ type);
}
return facty;
}
/**
* get cache pool by name ,caller should cache result
*
* @param poolName
* @return CachePool
*/
public CachePool getCachePool(String poolName) {
CachePool pool = allPools.get(poolName);
if (pool == null) {
throw new IllegalArgumentException("can't find cache pool:"
+ poolName);
} else {
return pool;
}
}
public void clearCache() {
logger.info("clear all cache pool ");
for (CachePool pool : allPools.values()) {
pool.clearCache();
}
}
}