This article for the original article, source code for the original code, such as reprint/copy, please in the page/code location clearly marked the original name, author and website, thank you!
Development tools: VS2017
Language: C #
dotnet version:. Net FrameWork 4.5 and above
System: Win10 X64
I. Installing a REDIS server
First, download the Redis server from the following Web site,
Https://github.com/MicrosoftArchive/redis/releases
and download Redis-x64-3.2.100.msi from the list below, as shown in:
Or from the Baidu network disk to download, as follows:
Https://pan.baidu.com/s/1dFya9ep
Next, install Redis-x64-3.2.100.msi, click the positive button all the while until the installation is complete, as shown in:
After installation, use Win+r to eject the "Run" form, enter "Services.msc" to open the system "services" form, we can see the following information:
The above is the Redis Server service, if you want to use this as a server, we strongly recommend that you set the startup type of the service to "automatic" (keep the default value)
Then, add the C:\Program Files\redis path to the system environment variable, as shown in:
The purpose of this is to no longer enter the file path in the future (reducing the inconvenience caused by the input path), as shown in:
Second, install Stackexchange.redis, for C # to interact with the server
First, install reference Stackexchange.redis from the Package Manager console by entering the following, as follows:
Install-package Stackexchange.redis
Note: The latest version is not allowed. DotNet4.0, use the. Net4.5 here, or it will not be installed.
The installation results are as follows:
Next, write the following code in the console:
usingStackexchange.redis;usingSystem;usingSystem.Threading;namespaceredisconsoleapp{classProgram {Static voidMain (string[] args) {Connectionmultiplexer cm= Connectionmultiplexer.connect ("127.0.0.1:6379"); Idatabase DB=cm. Getdatabase (); Db. Stringset ("Info","Hello World"); stringresult = db. Stringget ("Info"); Console.WriteLine (result); Isubscriber SC=cm. Getsubscriber (); stringChannelstr ="CH1"; Sc. Subscribe (Channelstr, (channel, information)= Console.WriteLine ($"From {Channel}: {Information}")); Sc. Publish (Channelstr,"hello,my name is Cnxy"); Thread.Sleep ( -); Sc. Publish (Channelstr,"My website is http://www.cnc6.cn"); Console.readkey (); } }}
The output results are as follows:
We can then also use REDIS-CLI to view the information we just created, as follows:
Other operations on hash tables, lists, collections and ordered collections, please Baidu, thank you!
[C #] uses Redis to store key-value pairs (Key-value pair)