📄 ehcache.java
字号:
/*
* Created on 2007-5-1
* Last modified on 2007-5-1
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.cache;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;
public class EHCache implements Cache{
private static final Logger logger = Logger.getLogger(EHCache.class);
private static final int SIXTY_THOUSAND_MS = 60000;
private net.sf.ehcache.Cache cache;
public EHCache(net.sf.ehcache.Cache ehcache){
cache = ehcache;
}
public Object read(Object key) {
return get(key);
}
public Object get(Object key) {
try {
if ( key == null ) {
return null;
}
else {
Element element = cache.get( key );
if ( element == null ) {
logger.debug( "Element for " + key + " is null!" );
return null;
}
else {
return element.getObjectValue();
}
}
}
catch (net.sf.ehcache.CacheException e) {
logger.error(e.toString());
return null;
}
}
public void put(Object key, Object value){
try {
Element element = new Element( key, value );
cache.put( element );
}
catch (IllegalArgumentException e) {
logger.error(e.toString());
}
catch (IllegalStateException e) {
logger.error(e.toString());
}
catch (net.sf.ehcache.CacheException e) {
logger.error(e.toString());
}
}
public void update(Object key, Object value) {
put(key,value);
}
public void remove(Object key) {
try {
cache.remove( key );
}
catch (ClassCastException e) {
logger.error(e.toString());
}
catch (IllegalStateException e) {
logger.error(e.toString());
}
catch (net.sf.ehcache.CacheException e) {
logger.error(e.toString());
}
}
public void clear() {
try {
cache.removeAll();
}
catch (IllegalStateException e) {
logger.error(e.toString());
}
catch (net.sf.ehcache.CacheException e) {
logger.error(e.toString());
}
}
public void destroy() {
try {
cache.getCacheManager().removeCache( cache.getName() );
}
catch (IllegalStateException e) {
logger.error(e.toString());
}
catch (net.sf.ehcache.CacheException e) {
logger.error(e.toString());
}
}
public void lock(Object key) {
}
public void unlock(Object key) {
}
public long nextTimestamp() {
return Timestamper.next();
}
public int getTimeout() {
return Timestamper.ONE_MS * SIXTY_THOUSAND_MS;
}
public String getRegionName() {
return cache.getName();
}
public long getMemoryStoreSize() {
try {
return cache.getMemoryStoreSize();
}
catch (Throwable t) {
return -1;
}
}
public long getDiskStoreSize(){
return cache.getDiskStoreSize();
}
public int getElementTotal(){
return cache.getSize();
}
/**
* 内存的缓存对象的个数
*/
public int getElementCountInMemory() {
return -1;
}
/**
* 缓存在硬盘的对象个数
*/
public int getElementCountOnDisk() {
return -1;
}
@SuppressWarnings("unchecked")
public Map toMap() {
try {
Map result = new HashMap();
Iterator iter = cache.getKeys().iterator();
while ( iter.hasNext() ) {
Object key = iter.next();
result.put( key, cache.get( key ).getObjectValue() );
}
return result;
}
catch (Exception e) {
logger.error(e.toString());
return null;
}
}
public Map getAll() {
return null;
}
public String toString(){
return cache.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -