Stackexchange is a good C # Redis client, but there are some drawbacks to the operation, and the use of StackExchange.Redis.Extensions has become a highly recommended option for simplifying operations. It makes it quite easy to use Stackexchange.
StackExchange.Redis.Extensions GitHub Address: https://github.com/imperugo/StackExchange.Redis.Extensions
The first step is to install the relevant package with NuGet:
Pm> Install-package StackExchange.Redis.Extensions.Newtonsoft
You can use it when you're done.
The following is a demonstration of a console case that modifies a value with multithreading.
usingSystem;usingSystem.Threading;usingStackExchange.Redis.Extensions.Core;usingStackExchange.Redis.Extensions.Core.Configuration;usingStackExchange.Redis.Extensions.Newtonsoft;namespaceredis{classProgram { Public Static voidIns () {thread thread=NewThread (() = { for(inti =0; I < +; i++) {client. Database.stringincrement ("Key"); } }); Thread. Start (); } Private Staticstackexchangerediscacheclient Client; Static voidMain (string[] args) { varRedisconfiguration =Newredisconfiguration ()//configuration {Hosts=Newredishost[] {NewRedishost () {Host ="127.0.0.1", Port =6379} } }; Client=NewStackexchangerediscacheclient (NewNewtonsoftserializer (), redisconfiguration); Client. ADD ("Key",0); for(intj =0; J <Ten; J + +) {Ins (); } thread.sleep ( -); inti = client. get<int> ("Key"); Console.WriteLine (i); Console.readkey (); } }}
It is worth noting that if you want to write the relevant configuration into app. Config or Web. config, you need to install another package: StackExchange.Redis.Extensions.LegacyConfiguration
Install-Package StackExchange.Redis.Extensions.LegacyConfiguration
The configuration file will then be able to write the relevant configuration:
<?XML version= "1.0" encoding= "Utf-8"?><Configuration><!--Note that the configsections must be the first child element of the configuration node - <configsections> < Sectionname= "Rediscacheclient"type= "StackExchange.Redis.Extensions.LegacyConfiguration.RedisCachingSectionHandler, StackExchange.Redis.Extensions.LegacyConfiguration " /> </configsections> <rediscacheclientAllowadmin= "true"SSL= "false"ConnectTimeout= " the"Database= "+"> <ServerenumerationstrategyMode= "single"Targetrole= "Preferslave"unreachableserveraction= "Ignoreifotheravailable" /> <hosts> <AddHost= "127.0.0.1"Cacheport= "6379" /> </hosts> </rediscacheclient></Configuration>
Then get the configuration in the code with Rediscachingsectionhandler.getconfig ():
var config =new stackexchangerediscacheclient (new Newtonsoftserializer (), config);
C # Redis Operations Class Library recommendation: StackExchange.Redis.Extensions