<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    當柳上原的風吹向天際的時候...

    真正的快樂來源于創造

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
    Cache的作用:
    在并發環境中減少重復性的對后臺的調用,從而提高運行效率。

    Cache必須要做到的:
    1.保證Cache中的數據與后臺系統數據的一致性。
    2.Cache必須要減少訪問后臺的操作以提高效率。
    如果不能同時做到以上兩點,則Cache失去了意義。

    Cache的基本操作:
    Read(Get):按鍵值從Cache中取得想要的數據;
    Write(Put):將鍵值對放到Cache中;
    Reset(RemoveAll):清除Cache中的原有數據;
    IsExist(has):判斷某鍵在Cache中是否存在。

    Cache中對數據的保護:
    因為Cache多用于多線程環境中,需要對數據進行保護;
    同步方法,同步塊能起到保護的作用,但是效率不高;
    讀寫鎖也能起到保護的左右,而且效率較高。

    Cache數據的時效性:
    使用過期數據是危險的,我們必須確保取出的數據的時效性;
    當放入數據時我們可以記錄放入的時間,這樣取出時就能得到它,然后與當前時間進行比較,如果小于一定間隔則可以認為數據沒有時效。間隔一般可以設置為后臺數據被更新間隔的平均時間的一半。

    Cache的設計:
    1.Data類,用以存儲數據和放入時間:
    import java.io.Serializable;
    import java.util.Date;

    public class Data implements Serializable{
        
    private static final long serialVersionUID = 8596587790584575734L;
        
        
    private byte[] value;

        
    private Date date;
        
        
    public Data(byte[] value,Date date){
            
    this.value=value;
            
    this.date=date;
        }
        
        
    public byte[] getValue() {
            
    return value;
        }

        
    public void setValue(byte[] value) {
            
    this.value = value;
        }

        
    public Date getDate() {
            
    return date;
        }

        
    public void setDate(Date date) {
            
    this.date = date;
        }
    }

    2.ReadWriteLock,用以在并發環境中保護數據:
    /**
     * Read-write Lock Class
     * 
     * 
    @author heyang
     * @time Sep 23, 2011,3:55:55 PM
     
    */
    public class ReadWriteLock {
        
    private boolean isRead;
        
    private boolean isWrite;
        
        
    /**
         * Add Read Lock
         
    */
        
    public synchronized void readLock(){
            
    while(isWrite){
                
    try{    
                    wait();
                }
                
    catch(InterruptedException ex){
                    ex.printStackTrace();
                }
            }
            
            isRead
    =true;
        }
        
        
    /**
         * Unlock the read-lock
         
    */
        
    public synchronized void readUnlock(){
            isRead
    =false;
            notifyAll();
        }
        
        
    /**
         * Add write lock
         
    */
        
    public synchronized void writeLock(){
            
    while(isRead){
                
    try{    
                    wait();
                }
                
    catch(InterruptedException ex){
                    ex.printStackTrace();
                }
            }
            
            
    while(isWrite){
                
    try{    
                    wait();
                }
                
    catch(InterruptedException ex){
                    ex.printStackTrace();
                }
            }
            
            isWrite
    =true;
        }
        
        
    /**
         * Unlock the write-lock
         
    */
        
    public synchronized void writeUnlock(){
            isWrite
    =false;
            notifyAll();
        }
    }

    3.AbstractCacheMap,用以提供Cache的接口和操作:
    import java.util.Date;


    /**
     * Parent class of InnerCacheMap & OutterCacheMap
     * 
     * 
    @author heyang
     * @time Sep 23, 2011,4:00:45 PM
     
    */
    public abstract class AbstractCacheMap {
        
    private static final int Time_Limit_300 = 300;
        
        
    // Read-Write lock,for protecting the cacheMap in the Multi-thread environment
        private ReadWriteLock lock;
        
        
    /**
         * Contructor
         
    */
        
    public AbstractCacheMap() {
            lock 
    = new ReadWriteLock();
        }
        
        
    /**
         * Put key-value pair into cacheMap
         * It can be called by any class
         * 
    @param key
         * 
    @param value
         * 
    @throws Exception
         
    */
        
    public void writeData(String key,byte[] value) throws Exception{
            
    try {
                lock.writeLock();
                set(key,
    new Data(value,new Date()));
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Put key-value pair into cacheMap,force child-class to implement.
         * It only can be called by child-class
         * 
    @param key
         * 
    @param value
         
    */
        
    protected abstract void set(String key,Data data) throws Exception;

        
    /**
         * Get value by it's key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public byte[] readData(String key) throws Exception{
            
    try {
                lock.readLock();
                Data data
    =get(key);
                
    return data.getValue();
            } 
    finally {
                lock.readUnlock();
            }
        }
        
        
    /**
         * Get value by it's key,force child-class to implement.
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract Data get(String key) throws Exception;
        
        
    /**
         * Judge the existence of a key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public boolean containsKey(String key) throws Exception{
            
    try {
                lock.readLock();
                
                
    if(contains(key)){
                    Data data
    =get(key);
                    
                    
    return isexceedTimeLimit(data.getDate());
                }
                
    return false;
            } 
    finally {
                lock.readUnlock();
            }
        }
        
        
    /**
         * Judge the existence of a key,force child-class to implement.
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract boolean contains(String key) throws Exception;
        
        
    /**
         * Remove a key-value pair from cacheMap by it's key
         * It can be called by any class
         * 
         * 
    @param key
         * 
    @throws Exception
         
    */
        
    public void removeData(String key) throws Exception{
            
    try {
                lock.writeLock();
                remove(key);
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Remove a key-value pair from cacheMap by it's key
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract void remove(String key) throws Exception;
        
        
    /**
         * Remove all data in the cacheMap
         * It can be called by any class
         * 
         * 
    @throws Exception
         
    */
        
    public void removeAllData() throws Exception{
            
    try {
                lock.writeLock();
                removeAll();
            } 
    finally {
                lock.writeUnlock();
            }
        }
        
        
    /**
         * Remove all data in the cacheMap
         * It only can be called by child-class
         * 
         * 
    @param key
         * 
    @return
         * 
    @throws Exception
         
    */
        
    protected abstract void removeAll() throws Exception;
        
        
    /**
         * Judge the date whether exceed the time limit
         * 
         * 
    @param date
         * 
    @return
         
    */
        
    private static boolean isexceedTimeLimit(Date date){
            
    long diff = (new Date()).getTime() - date.getTime();
            
            
    return diff<Time_Limit_300;
        }
    }

    4.InnerCacheMap,用HashMap作為實際內存空間的Cache實現類:
    import java.util.HashMap;
    import java.util.Map;

    import cachemap.base.AbstractCacheMap;
    import cachemap.base.Data;

    /**
     * CacheMap used local HashMap
     * 
     * 
    @author heyang
     * @time Sep 23, 2011,3:48:17 PM
     
    */
    public class InnerCacheMap extends AbstractCacheMap{
        
    // essential storage
        private Map<String,Data> map;
        
        
    /**
         * Contructor
         
    */
        
    public InnerCacheMap(){
            
    super();
            map
    =new HashMap<String,Data>();
        }

        @Override
        
    protected Data get(String key) throws Exception {
            
    return map.get(key);
        }

        @Override
        
    protected void set(String key, Data value) throws Exception {
            map.put(key, value);
        }

        @Override
        
    protected boolean contains(String key) throws Exception {
            
    return map.containsKey(key);
        }

        @Override
        
    protected void remove(String key) throws Exception {
            map.remove(key);
        }

        @Override
        
    protected void removeAll() throws Exception {
            map.clear();
        }
    }

    5.CacheMapSetter類,用于向Cache中異步寫入數據:
    import cachemap.base.AbstractCacheMap;


    /**
     * CacheMapSetter
     * It's use is to set a key-value pair into cacheMap
     * 
     * 
    @author heyang
     * @time Sep 26, 2011,10:11:36 AM
     
    */
    public final class CacheMapSetter implements Runnable{
        
    // The reference to the cacheMap
        private AbstractCacheMap cacheMap;
        
    private String key;
        
    private byte[] value;
        
        
    /**
         * Constuctor
         * 
    @param cacheMap
         * 
    @param key
         * 
    @param value
         
    */
        
    public CacheMapSetter(AbstractCacheMap cacheMap,String key,byte[] value){
            
    this.cacheMap=cacheMap;
            
    this.key=key;
            
    this.value=value;
            
            
    new Thread(this).start();
        }

        @Override
        
    public void run(){
            
    try{
                cacheMap.writeData(key, value);        
            }
            
    catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }


    6.使用示例:
                // Get key
                String key=getKey(inputEnv);
                
                
    // Get operation
                String operation=getOperation(inputEnv);
                
                
                
    if("READ".equalsIgnoreCase(operation)){
                    
    if(cacheMap.containsKey(key)){
                        
    byte[] value=cacheMap.readData(key);
                        
                        
    // TODO:
                       .....   
                    }
    else{
                        
    // TODO:
                        ... 
                    }
                }
    else if("WRITE".equalsIgnoreCase(operation)){
                    
    if(cacheMap.containsKey(key)){
                        cacheMap.removeData(key);
                    }    
                    
                    
    byte[] value=getValue(outputEnv);
                    
    new CacheMapSetter(cacheMap,key, value);
                    
                    
    // TODO:
                    ....               
                }
    else if("REMOVEALL".equalsIgnoreCase(operation)){
                    cacheMap.removeAllData();
                    
                    
    // TODO:
                    ....
                }
    else{
                    
    throw new Exception("Unknown Operation:"+operation);
                }

    以上類的代碼下載:
    posted on 2011-09-27 15:32 何楊 閱讀(539) 評論(0)  編輯  收藏 所屬分類: WMB
    主站蜘蛛池模板: 99国产精品视频免费观看| 亚洲码国产精品高潮在线| 日韩中文字幕免费视频| 黄色网页免费观看| 亚洲一区无码中文字幕乱码| 国产亚洲精品a在线无码| 四虎成人精品在永久免费| 日本免费网址大全在线观看| 久久精品成人免费观看| 久99久无码精品视频免费播放| 亚洲欧美乱色情图片| 911精品国产亚洲日本美国韩国| 国产午夜亚洲精品午夜鲁丝片| 国产中文字幕免费| 好爽…又高潮了免费毛片| 中文字幕免费在线看线人 | 50岁老女人的毛片免费观看| 亚洲免费一区二区| 伊人久久国产免费观看视频| 黄色毛片免费观看| 国产亚洲蜜芽精品久久| 亚洲精品国产高清在线观看| 亚洲综合伊人制服丝袜美腿| 亚洲福利视频一区二区三区| 91亚洲va在线天线va天堂va国产| 亚洲一二成人精品区| 久久久亚洲精品视频| 久久久无码精品亚洲日韩蜜桃 | 在线播放国产不卡免费视频| 无码天堂亚洲国产AV| 亚洲精品色在线网站| 亚洲AV成人片无码网站| 亚洲丁香婷婷综合久久| 亚洲欧美综合精品成人导航| 亚洲另类无码专区首页| 国产精品亚洲一区二区三区在线观看| 亚洲AV无码精品国产成人| 香蕉视频免费在线| 国产精品免费久久久久久久久 | 在线观看国产区亚洲一区成人| 久久精品国产亚洲Aⅴ蜜臀色欲|