支援 .NET Core 的 Memcached 用戶端 EnyimMemcachedCore

來源:互聯網
上載者:User

標籤:client   pack   result   build   hub   原始碼   void   add   password   

1. 介紹

EnyimMemcachedCore 是一個支援 .NET Core 的 Memcached 用戶端,是從 EnyimMemcached 遷移至 .NET Core的,原始碼託管在 GitHub 上:https://github.com/cnblogs/EnyimMemcachedCore ,NuGet 包地址:https://www.nuget.org/packages/EnyimMemcachedCore 。

2. 使用說明2.1 安裝 NuGet 包
Install-Package EnyimMemcachedCore
2.2 配置2.2.1 在 appsetting.json 中進行配置

1)不帶驗證的配置

{  "enyimMemcached": {    "Servers": [      {        "Address": "memcached",        "Port": 11211      }    ]  }}

2)帶驗證的配置

{  "enyimMemcached": {    "Servers": [      {        "Address": "memcached",        "Port": 11211      }    ],    "Authentication": {      "Type": "Enyim.Caching.Memcached.PlainTextAuthenticator",      "Parameters": {        "zone": "",        "userName": "username",        "password": "password"      }    }  }}

3)Startup.cs 中的配置代碼

public class Startup{    public void ConfigureServices(IServiceCollection services)    {        services.AddEnyimMemcached(options => Configuration.GetSection("enyimMemcached").Bind(options));    }    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)    {         app.UseEnyimMemcached();    }}
2.2.2 直接寫入程式碼配置(無需設定檔)

Startup.cs 中的寫入程式碼配置代碼

public class Startup{    public void ConfigureServices(IServiceCollection services)    {        services.AddEnyimMemcached(options =>        {            options.AddServer("memcached", 11211);                            //options.AddPlainTextAuthenticator("", "usename", "password");        });    }            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)    {        app.UseEnyimMemcached();    }}
2.3 調用

2.3.1 使用 IMemcachedClient 介面

public class TabNavService{    private ITabNavRepository _tabNavRepository;    private IMemcachedClient _memcachedClient;    public TabNavService(        ITabNavRepository tabNavRepository,        IMemcachedClient memcachedClient)    {        _tabNavRepository = tabNavRepository;        _memcachedClient = memcachedClient;    }    public async Task<IEnumerable<TabNav>> GetAll()    {        var cacheKey = "aboutus_tabnavs_all";        var result = await _memcachedClient.GetAsync<IEnumerable<TabNav>>(cacheKey);        if (!result.Success)        {            var tabNavs = await _tabNavRepository.GetAll();            await _memcachedClient.AddAsync(cacheKey, tabNavs, 300);            return tabNavs;        }        else        {            return result.Value;        }    }}

2.3.2 使用 IDistributedCache 介面(來自 Microsoft.Extensions.Caching.Abstractions )

public class CreativeService{    private ICreativeRepository _creativeRepository;    private IDistributedCache _cache;    public CreativeService(        ICreativeRepository creativeRepository,        IDistributedCache cache)    {        _creativeRepository = creativeRepository;        _cache = cache;    }    public async Task<IList<CreativeDTO>> GetCreatives(string unitName)    {        var cacheKey = $"creatives_{unitName}";        IList<CreativeDTO> creatives = null;        var creativesJson = await _cache.GetStringAsync(cacheKey);        if (creativesJson == null)        {            creatives = await _creativeRepository.GetCreatives(unitName)                    .ProjectTo<CreativeDTO>().ToListAsync();            var json = string.Empty;            if (creatives != null && creatives.Count() > 0)            {                json = JsonConvert.SerializeObject(creatives);            }            await _cache.SetStringAsync(                cacheKey,                 json,                 new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(5)));        }        else        {            creatives = JsonConvert.DeserializeObject<List<CreativeDTO>>(creativesJson);        }        return creatives;    }}
3. 問題支援

如果在使用中遇到問題,麻煩您在 GitHub 上提交 Issue 。

支援 .NET Core 的 Memcached 用戶端 EnyimMemcachedCore

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.