1. 程式人生 > 其它 >SequoiaDB巨杉資料庫-removeCoordRG()

SequoiaDB巨杉資料庫-removeCoordRG()

技術標籤:設計模式設計模式

引言

一級快取和二級快取大家應該不陌生。下面我們看一個一級快取和二級快取
二級快取
在這裡插入圖片描述

客戶端查詢資料,先查Redis,如果Redis中沒有的話再去EHCache中去查詢如果EHCache也沒有則會去資料庫中查詢,然後分別再快取到前兩級快取中

一級快取
在這裡插入圖片描述

我們想一下早期只有一級快取但是後來有二級快取了我們想加入進去,怎麼保證在不修改一級快取的情況下加入二級快取呢?
這就用到了我們的裝飾者模式

裝飾者模式的定義

  1. 抽象元件:定義一個抽象介面來規範準備附加功能的類
  2. 具體元件:將要被附加功能的類實現抽象構建角色介面
  3. 抽象裝飾者:持有具體構件角色的引用定義與抽象構件角色一致的介面
  4. 具體裝飾:實現抽象裝飾者角色,負責對具體構建新增額外功能

裝飾者模式的特徵

不改變原有功能實現增強

裝飾者模式的演示

就以我們上邊說的一級快取要擴充套件二三快取為例
1、定義抽象元件

public interface ComponentCache {
     Object getCache(String key);
}

2、實現一級快取(用Map去模擬)

public class OneLevelCacheComponent implements ComponentCache {
    //模擬以及快取
    HashMap oneLevel = new HashMap();

    {
oneLevel.put("test", "rwnb"); oneLevel.put("test1", "rwnb1"); } @Override public Object getCache(String key) { System.out.println("執行到了一級快取"); Object value = oneLevel.get(key); return value; }
}

3、在不動之前一級快取的基礎上加二三級快取先定義我們的抽象裝飾者

public abstract class AbstractDecorator implements ComponentCache {
    protected ComponentCache baseCacheComponent;

    public AbstractDecorator(ComponentCache oneLevelCacheComponent) {
        this.baseCacheComponent = oneLevelCacheComponent;
    }
}

4、去具體裝飾
二級快取

public class TwoDecorator extends AbstractDecorator {

    Map twoLevel = new HashMap();

    {
        twoLevel.put("test", "rwnb");
    }

    public TwoDecorator(OneLevelCacheComponent oneLevelCacheComponent) {
        super(oneLevelCacheComponent);
    }


    @Override
    public Object getCache(String key) {
        //1.先查詢二級快取,如果二級快取沒有查詢一級快取
        System.out.println("呼叫二級快取去查詢");
        Object twoValue = twoLevel.get(key);
        if (twoValue == null) {
            System.out.println("二級快取查不到");
            Object oneValue = baseCacheComponent.getCache(key);
            if (oneValue != null) {
                twoLevel.put(key, oneValue);
                twoValue = oneValue;
            }
        }
        //2.如果一級快取有得話存到二級快取
        return twoValue;
    }



}

三級快取

public class ThreeDecorator extends AbstractDecorator {

    Map threeLevel = new HashMap();

    {
        threeLevel.put("test", "rwnb");
    }

    public ThreeDecorator(TwoDecorator twoDecorator) {
        super(twoDecorator);
    }


    @Override
    public Object getCache(String key) {
        //1.先查詢三級快取,如果三級快取沒有查詢二級快取
        System.out.println("呼叫三級快取去查詢");
        Object threeValue = threeLevel.get(key);
        if (threeValue == null) {
            System.out.println("三級快取查不到");
            Object oneValue = baseCacheComponent.getCache(key);
            if (oneValue != null) {
                threeLevel.put(key, oneValue);
                threeValue = oneValue;
            }
        }
        //2.如果一級快取有得話存到二級快取
        return threeValue;
    }


}

5、建立工廠去測試

public class FactoryCache {

    public static ComponentCache getComponentCache() {
        ThreeDecorator threeDecorator=new ThreeDecorator(new TwoDecorator(new OneLevelCacheComponent()));   
        return threeDecorator;
    }

    public static void main(String[] args) {
        ComponentCache componentCache= getComponentCache();
        System.out.println(componentCache.getCache("test1"));
        System.out.println(componentCache.getCache("test1"));
    }
}

6、測試結果

呼叫三級快取去查詢
三級快取查不到
呼叫二級快取去查詢
二級快取查不到
執行到了一級快取
rwnb1

呼叫三級快取去查詢
rwnb1

可以看到第一次我們查到了一級快取才查到資料、但是第二次的話直接就從三級快取區查詢到了