CacheValue封裝的對象 支持時效
/**
- 封裝的緩存對象
- @author Administrator
- @param
- */
- public class CacheValue {
- private V value; //真實數據對象
- private long times; //緩存到期時間
- public V getValue() {
- return value;
- }
- public void setValue(V value) {
- this.value = value;
- }
- public long getTimes() {
- return times;
- }
- public void setTimes(long times) {
- this.times = times;
- }
}
CacheMap緩存Map集合
/**
- 緩存的map對象
- @author Administrator
- @param
- */
- public class CacheMap{
- //緩存Map
- Map
cacheMap=new HashMap (); - //保證不能被外界 構造
- CacheMap(){
- }
- public void setex(String key,V value,long times){
- CacheValue cValue=new CacheValue();
- cValue.setTimes(System.currentTimeMillis()+times*1000);
- cValue.setValue(value);
- cacheMap.put(key, cValue);
- }
- public V getValue(String key){
- CacheValue cValue=cacheMap.get(key);
- if(cValue!=null && cValue.getTimes()>System.currentTimeMillis()){
- return cValue.getValue();
- }else if(cValue!=null && cacheMap.containsKey(key)){
- //為了確保數據被清理 再清理一次
- cacheMap.remove(key);
- }
- return null;
- }
- /**
- 獲取key集合
- @return
- */
- public Set getKeys(){
- return cacheMap.keySet();
- }
}
WatchClass 線程實現 守護線程清理過期數據(也可用java定時任務實現,但消耗更高):
/**
- 線程
- @author Administrator
- */
- public class WatchClass extends Thread {
- //獲取緩存Map
- CacheMap> cacheMap= CacheFactory.newChacheMap();
- @Override
- public synchronized void run() {
- //輪詢清理
- while(true){
- if(cacheMap.cacheMap!=null){
- try{
- //獲取所有的key
- Set keySet=cacheMap.cacheMap.keySet();
- //需要清理的key
- Set removeKey=new HashSet();
- //遍歷查看是否需要清理
- for (String string : keySet) {
- CacheValue> cValue= cacheMap.cacheMap.get(string);
- //如果有效時間小於系統當前時間 則表示需要清理
- if (cValue.getTimes()
- //記錄需要清理的數據
- removeKey.add(string);
- }
- }
- //清理數據
- for (String string : removeKey) {
- cacheMap.cacheMap.remove(string);
- }
- //釋放內存
- removeKey=null;
- }catch(Exception e){
} }}
- }
- }
- CacheFactory類 用於創建全局緩存對象:
- /**
- 緩存工廠,用於創建緩存集合 單例模式
- @author Administrator
- @param
- */
- @SuppressWarnings("all")
- public class CacheFactory {
- private static CacheMap cacheMap;
- private CacheFactory(){
- }
- /**
- 同步確保數據安全
- @return
- */
- public synchronized static CacheMap newChacheMap(){
- if(cacheMap==null){
- cacheMap=new CacheMap();
- //開啟守護線程 用於清理過式的數據
- WatchClass watch=new WatchClass();
- watch.setDaemon(true);
- watch.start();
- }
- return (CacheMap)cacheMap;
- }
- }
- Test 類用於測試緩存的使用
- public class Test {
- /**
- @param a
- @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- // TODO Auto-generated method stub
- CacheMap newChacheMap = CacheFactory.newChacheMap();
- for (int i = 1; i <= 1000; i++) {
- newChacheMap.setex("zhangsan"+i, i+"", (new Random()).nextInt(10)+1);
- }
- //newChacheMap.setex("lisi", "李四", 20);
- while(true){
- Thread.sleep(10);
- Set keys=newChacheMap.getKeys();
- System.out.println(newChacheMap.getValue("zhangsan"+(new Random()).nextInt(1000)));
- }
- }
- }
![純手寫java本地緩存機制,用於學習緩存原理](http://p2.ttnews.xyz/loading.gif)
閱讀更多 Java程序猿鼓勵師 的文章