MyCacheUtil.java
package com.sunrise.jop.common.util;
import java.io.File;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import test.TestHello;
/**
* 缓存工具类
* */
public class MyCacheUtil {
private static final int CACHE_TIME = 1000 * 5 ; //默认缓存 1分钟
private static Map cacheMap = new HashMap();
/**
* 缓存properties文件内容
* filename properties的路径
* key为properties中所取字段的key
* isForever 是否永久保存
* time有效时间 ,为null取默认时间(1分钟)
* */
public static Object propertiesCache(String filename, String key,boolean isForever,Integer time){
String mapKey = filename+File.separator+key;
Timestamp nowTime = MyTimeUtil.getNowTime();
if( cacheMap.containsKey(mapKey) ){
MyCache cache = cacheMap.get(mapKey);
if(cache.isForever()==true){
return cache.getObject();
}else{
if( cache.getDestroyTime() > nowTime.getTime()){ //未失效
return cache.getObject();
}
}
}
String result = PropertiesUtil.getProp(filename, key);
Timestamp destroyTime = null;
if(time==null||time<=0){
destroyTime = MyTimeUtil.getNextTime(nowTime, CACHE_TIME);
}else{
destroyTime = MyTimeUtil.getNextTime(nowTime, time);
}
MyCache cache = new MyCache(result,isForever,destroyTime.getTime());
cacheMap.put(mapKey, cache);
return result;
}
/**
* 缓存对象
* clazz为缓存的对象
* isForever是否永久缓存对象
* time有效时间 ,为null取默认时间(1分钟)
* @throws ClassNotFoundException
* */
public static Object objectCache(Class clazz,boolean isForever,Integer time) throws ClassNotFoundException{
if(clazz==null) return null;
String mapKey = clazz.getName();
Timestamp nowTime = MyTimeUtil.getNowTime();
if( cacheMap.containsKey(mapKey) ){
MyCache cache = cacheMap.get(mapKey);
if(cache.isForever()==true){
return cache.getObject();
}
else if( cache.getDestroyTime() > nowTime.getTime()){ //未失效
return cache.getObject();
}
}
Object object = null;
try {
object = Class.forName(clazz.getName()).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
Timestamp destroyTime = null;
if(time==null||time<=0){
destroyTime = MyTimeUtil.getNextTime(nowTime, CACHE_TIME);
}else{
destroyTime = MyTimeUtil.getNextTime(nowTime, time);
}
MyCache cache = new MyCache(object,isForever,destroyTime.getTime());
cacheMap.put(mapKey, cache);
return object;
}
}
MyTimeUtil.java
package com.sunrise.jop.common.util;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
/**
* 时间工具类
* */
public class MyTimeUtil {
//得到现在时间
public static Timestamp getNowTime(){
return new Timestamp(new Date().getTime());
}
/**
* 得到下一个时间
* currentTime 当前时间
* interval 时间间隔(毫秒)
* */
public static Timestamp getNextTime(Timestamp currentTime,int interval){
if(currentTime == null) return null;
int intervalSecond = interval / 1000;
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(currentTime.getTime()));
calendar.add(Calendar.SECOND, intervalSecond);
return new Timestamp(calendar.getTimeInMillis() );
}
//
}
package com.sunrise.jop.common.util;
/**
* 缓存类
* */
class MyCache{
private boolean isForever ;//是否永久
private long destroyTime; //销毁时间
private Object object; //缓存的对象
public MyCache(Object object, boolean isForever,long destroyTime) {
this.isForever = isForever;
this.destroyTime = destroyTime;
this.object = object;
}
public long getDestroyTime() {
return destroyTime;
}
public void setDestroyTime(long destroyTime) {
this.destroyTime = destroyTime;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public boolean isForever() {
return isForever;
}
public void setForever(boolean isForever) {
this.isForever = isForever;
}
}
TestMyCache.java
package test;
import static org.junit.Assert.*;
import org.junit.Test;
import com.sunrise.jop.common.util.MyCacheUtil;
public class TestMyCache {
// 测试缓存对象
@Test
public void testObjectCache() {
try{
TestHello testHello = (TestHello) MyCacheUtil.objectCache(TestHello.class, false,null);
System.out.println("testHello1 = "+ testHello);
testHello = (TestHello) MyCacheUtil.objectCache(TestHello.class, false,null);
System.out.println("testHello2 = "+ testHello);
Thread.sleep(1000*5*2);
testHello = (TestHello) MyCacheUtil.objectCache(TestHello.class,false, null);
System.out.println("testHello3 = "+ testHello);
}catch(Exception e){
e.printStackTrace();
}
}
// 测试缓存properties文件内容
@Test
public void testPropertiesCache() {
try{
String filename = "email.properties";
String key = "smtpHostName";
Object value = MyCacheUtil.propertiesCache(filename, key, false, null);
System.out.println("value1 = " + value+" "+value.toString());
value = MyCacheUtil.propertiesCache(filename, key, false, null);
System.out.println("value1 = " + value+" "+value.toString());
Thread.sleep(1000*5*2);
value = MyCacheUtil.propertiesCache(filename, key, false, null);
System.out.println("value1 = " + value+" "+value.toString());
}catch(Exception e){
e.printStackTrace();
}
}
}