缓存工具类MyCacheUtil
生活随笔
收集整理的這篇文章主要介紹了
缓存工具类MyCacheUtil
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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<String,MyCache> cacheMap = new HashMap<String,MyCache>();/*** 緩存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() ); }//}MyCache.java 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 {// 測試緩存對象@Testpublic 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文件內容@Testpublic 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();}}}總結
以上是生活随笔為你收集整理的缓存工具类MyCacheUtil的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (9) ab测试工具安装与使用
- 下一篇: oracle实现主键自动增长