1. Introduction
Enyimmemcachedcore is a Memcached client that supports. NET core, which is migrated from enyimmemcached to. NET core, and the source code is hosted on GitHub: HTTPS://GITHUB.COM/CNB Logs/enyimmemcachedcore, NuGet package address: Https://www.nuget.org/packages/EnyimMemcachedCore.
2. Install the NuGet package using instructions 2.1
Install-package Enyimmemcachedcore
2.2 Configuring 2.2.1 in Appsetting.json
1) Configuration without authentication
{" enyimmemcached": { "Servers": [ { "Address": "memcached", "Port": 11211 } ] }}
2) configuration with authentication
{" enyimmemcached": { "Servers": [ { "Address": "memcached", "Port": 11211 } ], " Authentication ": { " Type ":" Enyim.Caching.Memcached.PlainTextAuthenticator ", " Parameters ": { " zone ": "", "UserName": "UserName", "password": "Password" } }}}
3) configuration code in the Startup.cs
Public class startup{ publicvoid configureservices (iservicecollection services) { = = Configuration.getsection ("enyimmemcached"). Bind (options)); } Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { app. Useenyimmemcached (); }}
2.2.2 Direct hard-coded configuration (no configuration file required)
Hard-coded configuration code in Startup.cs
Public classstartup{ Public voidconfigureservices (iservicecollection services) {services. addenyimmemcached (Options={options. Addserver ("memcached",11211); //options. Addplaintextauthenticator ("", "usename", "password"); }); } Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, Iloggerfactory loggerfactory) {app. Useenyimmemcached (); }}
2.3 Calls
2.3.1 Using the Imemcachedclient interface
Public classtabnavservice{Privateitabnavrepository _tabnavrepository; Privateimemcachedclient _memcachedclient; PublicTabnavservice (itabnavrepository tabnavrepository, imemcachedclient memcachedclient) {_tabNa Vrepository=tabnavrepository; _memcachedclient=memcachedclient; } Public AsyncTask<ienumerable<tabnav>>GetAll () {varCacheKey ="Aboutus_tabnavs_all"; varresult =await _memcachedclient.getasync<IEnumerable<TabNav>>(CacheKey); if(!result. Success) {varTabnavs =await_tabnavrepository.getall (); await _memcachedclient.addasync(CacheKey, Tabnavs, -); returnTabnavs; } Else { returnresult. Value; } }}
2.3.2 using Idistributedcache interface (from Microsoft.Extensions.Caching.Abstractions)
Public classcreativeservice{Privateicreativerepository _creativerepository; PrivateIdistributedcache _cache; PublicCreativeservice (icreativerepository creativerepository, Idistributedcache cache) {_creative Repository=creativerepository; _cache=Cache; } Public AsyncTask<ilist<creativedto>> Getcreatives (stringunitname) { varCacheKey = $"Creatives_{unitname}"; IList<CreativeDTO> creatives =NULL; varCreativesjson =await_cache. Getstringasync (CacheKey); if(Creativesjson = =NULL) {Creatives=await_creativerepository.getcreatives (unitname). Projectto<CreativeDTO>(). Tolistasync (); varJSON =string. Empty; if(Creatives! =NULL&& creatives. Count () >0) {JSON=Jsonconvert.serializeobject (creatives); } await _cache. Setstringasync(CacheKey, JSON,NewDistributedcacheentryoptions (). Setslidingexpiration (Timespan.fromminutes (5))); } Else{Creatives= jsonconvert.deserializeobject<list<creativedto>>(Creativesjson); } returncreatives; }}
3. Problem support
If you are having trouble using it, please submit Issue on GitHub.
Support for. NET Core Memcached Client Enyimmemcachedcore