4.3 可配置的分布式緩衝(下)

來源:互聯網
上載者:User

標籤:soft   sof   config   ref   代碼   介紹   本地   方式   div   

上節,我們說明了封裝分布式緩衝的介面、配置以及在startup中的註冊方式。下面的,我們具體介紹下,分布式緩衝的具體實現。

第一個實現,將本機快取作為分布式緩衝,主要用於沒有分布式緩衝的情況,這樣就可以不修改程式的情況下直接使用本機快取。

 1     public class DefaultCacheHandler : BaseCacheHandler 2     { 3         private IDistributedCache memoryCache; 4  5         public DefaultCacheHandler(CachingConfigInfo configInfo) : base(configInfo) 6         { 7             this.memoryCache = new MemoryDistributedCache(new MemoryCache(new MemoryCacheOptions())); 8         } 9 10         protected override IDistributedCache _Cache11         {12             get13             {14                 return this.memoryCache;15             }16         }17     }

 

第二個實現,使用redis作為分布式緩衝。

 1     public class RedisCacheHandler : BaseCacheHandler 2     { 3         private IDistributedCache _RedisCache; 4  5         public RedisCacheHandler(CachingConfigInfo configInfo) : base(configInfo) 6         { 7             IOptions<RedisCacheOptions> optionsAccessor = new RedisCacheOptions(); 8  9             optionsAccessor.Value.Configuration = string.Join(",", _ConfigInfo.Servers.Select(s => s.HostName + ":" + s.Port));10             optionsAccessor.Value.InstanceName = "Runtime";11 12             _RedisCache = new RedisCache(optionsAccessor);13         }14 15         protected override IDistributedCache _Cache16         {17             get18             {19                 return _RedisCache;20             }21         }22     }

 

如果想使用Memcached等緩衝,具體做法是,首先根據IDistributedCache介面實現對於memcached的存取,然後參照根據以上實現BaseCacheHandler類即可。如何根據IDistributedCache介面實現對於memcached的存取,這是一個痛點,不過如果之前使用過.net 訪問memcached的時候,應該對Enyim.memcached不陌生吧。部落格園使用.net core改寫了Enyim.memcached,具體請見.NET跨平台之旅:基於.NET Core改寫EnyimMemcached,實現Linux上訪問memcached緩衝。

其實從這個類庫的原始碼我們可以發現,MemcachedClient是IDistributedCache介面的實現,因此這個我們拿過來直接使用就行,剩下再利用MemcachedClient編寫一個實現BaseCacheHandler類就萬事大吉了:)

面向雲的.net core開發架構目錄

 

MemcachedClient

4.3 可配置的分布式緩衝(下)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.