📄 cacheimpl.java
字号:
/*
* Created on 2007-2-26
* Last modified on 2007-05-14
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.cache.singleton;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import com.yeqiangwei.club.cache.Cache;
import com.yeqiangwei.util.Validator;
import org.apache.log4j.Logger;
public class CacheImpl implements Cache{
private static final Logger logger = Logger.getLogger(CacheImpl.class);
private String regionName;
//public static Map<String, Map> cacheMap;
public static Map<String, Map> CACHES;
static{
if(CACHES==null){
CACHES = init();
}
}
public CacheImpl(){
this.regionName = "YQWcache";
}
public CacheImpl(String regionName){
this.regionName = regionName;
}
public Object read(Object key) {
if(Validator.isEmpty(key)){
return null;
}else{
Object obj = this.getMap().get(key);
if(Validator.isEmpty(obj)){
CacheUtil.cacheMisses++;
}else{
CacheUtil.cacheHits++;
}
return obj;
}
}
public Object get(Object key) {
if(Validator.isEmpty(key)){
return null;
}else{
Object obj = this.getMap().get(key);
if(Validator.isEmpty(obj)){
CacheUtil.cacheMisses++;
}else{
CacheUtil.cacheHits++;
}
return obj;
}
}
@SuppressWarnings("unchecked")
public void put(Object key, Object value) {
if(!Validator.isEmpty(key)){
CacheUtil.cachePuts++;
this.getMap().put(key,value);
}
}
@SuppressWarnings("unchecked")
public void update(Object key, Object value) {
if(!Validator.isEmpty(key)){
CacheUtil.cachePuts++;
this.getMap().put(key,value);
}
}
public void remove(Object key) {
if(!Validator.isEmpty(key)){
this.getMap().remove(key);
}
}
public void clear() {
logger.debug("清除Cache regionName is "+regionName);
this.getMap().clear();
}
public void destroy() {
logger.debug("清除Cache regionName is "+regionName);
this.getMap().clear();
}
public void lock(Object key) {
}
public void unlock(Object key) {
}
public long nextTimestamp() {
return 0;
}
public int getTimeout() {
return 0;
}
public String getRegionName() {
return null;
}
public long getSizeInMemory() {
return 0;
}
public int getElementCountInMemory() {
return 0;
}
public int getElementCountOnDisk() {
return 0;
}
public Map toMap() {
return null;
}
public Map getMap() {
if(Validator.isEmpty(CACHES.get(regionName))){
logger.info("create singleton Cache, region is "+regionName);
CACHES.put(regionName, new HashMap());
return CACHES.get(regionName);
}else{
return CACHES.get(regionName);
}
}
/*
public static Map<String, Map> getCacheMap() {
if(Validator.isEmpty(cacheMap)){
return cacheMapInit();
}else{
return cacheMap;
}
}
*/
public Map getAll() {
return this.getMap();
}
private static synchronized Map<String, Map> init(){
logger.info("初始化Cache管理器!");
return CACHES = new Hashtable<String, Map>();
}
public long getMemoryStoreSize() {
return 0;
}
public long getDiskStoreSize() {
return 0;
}
public int getElementTotal() {
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -