📄 bdbcachemanager.java
字号:
package com.cache.bdb;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.cache.behavior.ICache;
import com.cache.behavior.ICacheManager;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.collections.StoredMap;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
public class BDBCacheManager implements ICacheManager {
private static Log log = LogFactory.getLog(BDBCacheManager.class);
private static BDBCacheManager manager;
static Properties pps = configure();
private Environment env;
private DatabaseConfig dbConfig;
private StoredClassCatalog javaCatalog;
private Map caches = null;
static Properties configure() {
// 配置优先级:环境变量(-Dbdbcache.home_dir...) > 配置文件 > 默认值
log.debug("Initializing Berkelay DB configuration");
Properties defaults = new Properties();
// set all default config here
defaults.setProperty("bdbcache.home_dir", "/opt/bdbcache");
defaults.setProperty("bdbcache.java_class_catalog", "java_class_catalog");
// end set default
InputStream is = BDBCacheManager.class.getResourceAsStream("/bdbcache.properties");
if (is != null) {
log.debug("Reading BDB configuration from 'bdbcache.properties'...");
Properties props = new Properties(defaults);
try {
props.load(is);
} catch (IOException e) {
String s = "Unable to load 'bdbcache.properties' due to IOException.";
log.error("Unable to load 'bdbcache.properties' due to IOException.", e);
throw new IllegalStateException(
"Unable to load 'bdbcache.properties' due to IOException.");
}
defaults = props;
} else {
log.info("Unable to load 'bdbcache.properties'... Using defaults.");
}
// read system enviroment
Properties sysProps = System.getProperties();
log.debug("Read BDB configuration from system properties");
for(Enumeration e = sysProps.keys(); e.hasMoreElements();){
String key = (String) e.nextElement();
if(key.startsWith("bdbcache.")){
defaults.put(key, sysProps.get(key));
}
}
if (log.isDebugEnabled()) {
log.debug("BDB configuration:");
for(Enumeration e = defaults.keys(); e.hasMoreElements();){
String key = (String) e.nextElement();
log.debug(key + "=" + defaults.getProperty(key));
}
}
return defaults;
}
/**
* Open storage environment and catalogs.
*/
void initialize(String homeDirectory) throws DatabaseException, FileNotFoundException {
// Open the Berkeley DB environment in transactional mode.
//
log.info("Opening environment in: " + homeDirectory);
/*Properties props = new Properties();
props.load(BDBCacheManager.class.getResourceAsStream("/je.properties"));*/
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
envConfig.setTxnWriteNoSync(true);
envConfig.setAllowCreate(true);
envConfig.setLockTimeout(2000000L);
env = new Environment(new File(homeDirectory), envConfig);
// Set the Berkeley DB config for opening all stores.
//
dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
// Create the Serial class catalog. This holds the serialized class
// format for all database records of serial format.
//
Database catalogDb = env.openDatabase(null, pps.getProperty("bdbcache.java_class_catalog"), dbConfig);
javaCatalog = new StoredClassCatalog(catalogDb);
}
private BDBCacheManager() {
try {
initialize(pps.getProperty("bdbcache.home_dir"));
caches = new Hashtable();
} catch (Exception e) {
log.error("Unable to build a BDBCacheManager.");
throw new IllegalStateException("Unable to build a BDBCacheManager.");
}
}
public static BDBCacheManager getInstance() {
if (manager == null) {
synchronized (BDBCacheManager.class) {
if (manager == null) {
BDBCacheManager newManager = new BDBCacheManager();
manager = newManager;
}
}
}
return manager;
}
/**
* Return the storage environment for the database.
*/
public final Environment getEnvironment() {
return env;
}
/**
* Return the class catalog.
*/
public final StoredClassCatalog getClassCatalog() {
return javaCatalog;
}
public ICache buildCache(String regionName, Class keyClass, Class valueClass, Properties properties) throws Exception {
Object obj = getCache(regionName);
ICache ret = null;
if (obj != null) {
ret = (ICache) obj;
} else {
synchronized (BDBCacheManager.class) {
if (ret == null) {
StoredMap sm = null;
try {
Database cacheDb = env.openDatabase(null, regionName,
dbConfig);
ret = new BDBCache(cacheDb, javaCatalog, keyClass, valueClass);
}
catch (DatabaseException de) {
log.error("Unable to build a BDB Cache : " + de.toString());
throw de;
}
putCache(regionName, ret);
}
}
}
return ret;
}
public ICache getCache(String regionName) {
return (ICache)caches.get(regionName);
}
public void putCache(String regionName, ICache cache) {
caches.put(regionName, cache);
}
public void removeCache(String regionName) {
caches.remove(regionName);
}
/**
* Close all databases and the environment.
*/
public void close() throws DatabaseException {
synchronized (caches) {
Iterator it = caches.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
BDBCache cache = (BDBCache) caches.get(key);
if (cache != null && cache.getDatabase() != null) {
cache.getDatabase().close();
}
}
}
javaCatalog.close();
env.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -