Class ManagedCache<K,V>

java.lang.Object
io.leandev.appfuse.cache.core.ManagedCache<K,V>
Type Parameters:
K - 鍵的型別
V - 值的型別
All Implemented Interfaces:
Cache<K,V>

public class ManagedCache<K,V> extends Object implements Cache<K,V>

可管理的快取

包裝底層快取,提供停用、清除、狀態查詢等管理功能。

啟用狀態(ADR-007):服務與否由兩個共享旗標的 AND 決定——manager 層總閘 (managerEnabled)與本快取個別開關(cacheEnabled)。兩旗標皆由 CacheManager 持有並注入, 故同名快取即使被 getCache() 重新包裝成新的 ManagedCache 實例,停用狀態仍一致、不會蒸發。

Since:
1.0
  • Constructor Details

    • ManagedCache

      public ManagedCache(Cache<K,V> delegate, CacheConfiguration<K,V> configuration, AtomicBoolean managerEnabled, AtomicBoolean cacheEnabled)
      以共享旗標建構(由 CacheManager 使用)
      Parameters:
      delegate - 底層快取
      configuration - 快取配置
      managerEnabled - manager 層總閘(共享旗標)
      cacheEnabled - 本快取個別開關(共享旗標)
    • ManagedCache

      public ManagedCache(Cache<K,V> delegate, CacheConfiguration<K,V> configuration)

      以獨立旗標建構(standalone 用途,如單元測試)

      總閘恆為啟用,個別開關自有一份;停用語意與 ADR-007 前一致。

      Parameters:
      delegate - 底層快取
      configuration - 快取配置
  • Method Details

    • get

      public V get(K key)
      Description copied from interface: Cache
      取得快取值
      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 鍵
      Returns:
      快取值,若不存在或快取已停用則返回 null
    • put

      public void put(K key, V value)
      Description copied from interface: Cache
      寫入快取
      Specified by:
      put in interface Cache<K,V>
      Parameters:
      key - 鍵
      value - 值
    • remove

      public void remove(K key)
      Description copied from interface: Cache
      移除快取項目
      Specified by:
      remove in interface Cache<K,V>
      Parameters:
      key - 鍵
    • clear

      public void clear()
      Description copied from interface: Cache
      清空所有快取項目
      Specified by:
      clear in interface Cache<K,V>
    • containsKey

      public boolean containsKey(K key)
      Description copied from interface: Cache
      檢查快取是否包含指定鍵
      Specified by:
      containsKey in interface Cache<K,V>
      Parameters:
      key - 鍵
      Returns:
      true 若包含該鍵
    • disable

      public void disable()
      Description copied from interface: Cache

      停用快取

      停用後:

      • get() 操作返回 null
      • put() 操作被忽略
      • 已存在的資料保留
      • 可透過 enable() 重新啟用
      Specified by:
      disable in interface Cache<K,V>
    • enable

      public void enable()
      Description copied from interface: Cache
      啟用快取
      Specified by:
      enable in interface Cache<K,V>
    • isEnabled

      public boolean isEnabled()
      回傳有效服務狀態(manager 總閘 AND 本快取個別開關)
      Specified by:
      isEnabled in interface Cache<K,V>
      Returns:
      true 若快取已啟用
    • getStatistics

      public CacheStatistics getStatistics()
      Description copied from interface: Cache
      取得快取統計資訊
      Specified by:
      getStatistics in interface Cache<K,V>
      Returns:
      統計資訊
    • getStatus

      public CacheStatus getStatus()
      Description copied from interface: Cache
      取得快取狀態
      Specified by:
      getStatus in interface Cache<K,V>
      Returns:
      狀態資訊
    • getName

      public String getName()
      Description copied from interface: Cache
      取得快取名稱
      Specified by:
      getName in interface Cache<K,V>
      Returns:
      快取名稱
    • getConfiguration

      public CacheConfiguration<K,V> getConfiguration()
      Description copied from interface: Cache
      取得快取配置
      Specified by:
      getConfiguration in interface Cache<K,V>
      Returns:
      配置資訊
    • getDelegate

      public Cache<K,V> getDelegate()
      取得底層快取(供進階使用)
      Returns:
      底層快取