Original: [C #] using Redis to store key-value pairs (Key-value pair)
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:
Third, how to encrypt the connection
First, open "C:\Program files\redis\redis.windows.conf", the Inside of the "# Requirepass foobared" changed to "#requirepass your password" can be changed here to " Requirepass 123 ", after modification, the service must be restarted as shown in:
Second, in C # source code, Connectionmultiplexer.connect ("127.0.0.1:6379") is changed to Connectionmultiplexer.connect ("127.0.0.1:6379, Password=123 ").
Then, let's see if it's protected by a password, as shown in:
As you can see, the connection has been protected!
Iv. How to use remote access
First, open "C:\Program files\redis\redis.windows.conf", the inside of the "bind 127.0.0.1" changed to "#bind 127.0.0.1" can be modified after the service must be restarted, as shown in:
Then, use a different IP address (such as 192.168.94.250) to operate as shown in the following:
As can be seen, the use of 192.168.94.250 this IP address can be operated normally.
Similarly, the corresponding IP address in C # code needs to be changed from 127.0.0.1 to 192.168.94.250, as shown in the following code:
// connectionmultiplexer cm = Connectionmultiplexer.connect ("127.0.0.1:6379,password=123"); Connectionmultiplexer cm = connectionmultiplexer.connect ("192.168.94.250:6379,password=123 ");
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)