Redis Learning Notes (C #)

Source: Internet
Author: User

Redis Installation and simple operation

Installation steps under Windows:

1. The first step is, of course, download first. Address: Https://github.com/dmajkic/redis/downloads

(according to their actual situation, choose whether the next 32 or 64-bit)

Downloaded files such as:

2. Open the service side of Redis

Method One: Double-click Redis-server.exe (This is the simplest and most brutal way)

Method Two: Can also be opened through the command line (such as my files are placed in the D:\Program\Redis\Redis\redis-2.4.5-win32-win64\64bit)

Typing the following command at the command line (the directory needs to be changed to your own file location)

Either way, as shown in the end, the Server Started, the service side, has already turned on

This time note that this CMD window does not close, this is the server, let him have been waiting here ....

3. Open the Client

With the CD command, switch to the file saved directory,

Then enter Redis-cli.exe-h 127.0.0.1-p 6379 (as the server is on my local side, So directly with 127.0.0.1, if it is a remote server, the input server ip;6379 is the Redis default port number, you can also modify the port number configuration in redis.conf)

Direct, more intuitive

All preparations have been completed.

Get started on our Redis tour ~~~~~~

The value corresponding to the set TestKey1 1qaz save Key (TestKey1) is entered on the client (1qaz)

And get the value of TestKey1 by get

Ho~ very excited, set get all executed successfully!

The problem is, the command line is operational, but how does my program operate Redis?

I'll use. NET C # as an example to explain how to operate Redis through C #

Using Redis in. Net

In fact, Redis is a third-party driver that can support multiple languages C#,php,java ....

Using Redis in. NET, there are two officially recommended: Servicestack.redis and Stackexchange.redis

using Redis's Servicestack.redis in. NET

Servicestack.redis:https://github.com/servicestack/servicestack.redis

If the above address download failed, go directly to my bag Http://files.cnblogs.com/files/zhangddleon/ServiceStack.Redis.zip

4 DLLs to use after downloading

Now, let's create a new console program and reference the 4 DLLs above to our project

classProgram {Private Static voidMain (string[] args) {            Try{redisclient redisclient=NewRedisclient ("127.0.0.1",6379); BOOLB1 = Redisclient.set ("TestKey1","testValue111"); BOOLB2 = Redisclient.set ("TestKey2","testValue222", DateTime.Now.AddSeconds (5));//set the expiration time to 5s                stringvalue1 = redisclient.get<string> ("TestKey1"); stringvalue2 = redisclient.get<string> ("TestKey2"); Console.WriteLine ("value1:"+ value1 +", value2:"+value2); Thread.Sleep (6000); stringValue22 = redisclient.get<string> ("TestKey2"); Console.WriteLine ("Value22:"+ Value22);//TestKey2 is expired, Value22 is emptyConsole.ReadLine (); }            Catch(Exception ex) {Throwex; }        }    }

This is the way to use Redis in. NET through Servicestack.

But now the latest version of the Servicestack has been commercialized, adding many restrictions, such as an hour up to 6000 requests, the official description is as follows:

Commit, some people use, began to charge!!

Despair, Servicestack charge, no stackexchange.

So let's just learn how to use Stackexchange.redis.

(The siege lion is the diligent spokesperson)

Using Redis's Stackexchange.redis in. Net

Installing Stackexchange.redis

Search Stackexchange.redis, install

After installation, open references to see if there is Stackexchange.redis

Code Farm Start Code

classProgram {Static voidMain (string[] args) {            //connectionmultiplexer Redis = Connectionmultiplexer.connect ("127.0.0.1");Connectionmultiplexer client = Clientmgr.getclient ("127.0.0.1"); Idatabase DB=client.            Getdatabase (); Db. Stringset ("TestKey1","AAA"); Db. Stringappend ("TestKey1","BBB"); stringValue = db. Stringget ("TestKey1");    Console.WriteLine (value); //Output AAABBB        }         Public classClientmgr {Private Static ReadOnly ObjectLocker =New Object(); Private Staticconnectionmultiplexer Client;  Public StaticConnectionmultiplexer Getclient (stringconnectionstr) {                if(Client = =NULL)                {                    Lock(locker) {if(Client = =NULL) {Client=Connectionmultiplexer.connect (CONNECTIONSTR); }                    }                }                returnclient; }        }            }

Well, that's the basic usage of Stackexchange.redis.

If you have any questions or suggestions, we can discuss together and make progress together.

Reference Link: http://www.runoob.com/redis/redis-install.html

Redis Learning Notes (C #)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.