Redis Study Notes (C #),

Source: Internet
Author: User

Redis Study Notes (C #),

Redis installation and simple operations

Installation Steps in Windows:

1. The first step is to download it first ~ Address: https://github.com/dmajkic/redis/downloads

(Select the next 32-bit or 64-bit based on your actual situation)

The downloaded files are as follows:

 

2. Open the Redis Server

Method 1: Double redis-server.exe (this is the simplest and most crude method)

Method 2: You can also open it through the command line (for example, I put these files in D: \ Program \ Redis \ redis-2.4.5-win32-win64 \ 64bit)

Enter the following command in the command line (the directory must be changed to the file location you put yourself)

Either way, as shown in the final figure, Server Started, that is, the Server has enabled

At this time, please note that the cmd window should not be closed. This is the server, so you have to keep him waiting here ....

3. Open the client

Run the cd command to switch to the file storage directory,

Enter redis-cli.exe-h 127.0.0.1-p 6379 (because the server is on my local computer, 127.0.0.1 is used directly. If the server is a remote server, enter the Server Ip address. 6379 is the default redis port number, or in redis. modify the port number configuration in conf)

Direct and intuitive

Now all preparations have been completed.

Start our redis journey ~~~~~~

 

Enter the value of set testKey1 1qaz storage key (testKey1) on the client (1qaz)

Get the value of testKey1 through get.

Ho ~ Very excited. set get is successfully executed!

 

The problem is, the command line can be operated, but how does my program operate redis?

I will use. net C # as an example to describe how to operate redis through C #.

 

Use Redis in. Net

In fact, redis is a third-party driver that supports multiple languages C #, PHP, JAVA ....

Using Redis in. Net, two officially recommended services are ServiceStack. Redis and StackExchange. Redis.

Use ServiceStack. Redis of Redis in. Net

ServiceStack. Redis: https://github.com/ServiceStack/ServiceStack.Redis

If the download from the above address fails, just download my package, http://files.cnblogs.com/files/zhangddleon/ServiceStack.Redis.zip.

After downloading, you need to use four dll files.

Now, we will create a new console program and reference the above four dll files to our project.

Class Program {private static void Main (string [] args) {try {RedisClient redisClient = new RedisClient ("127.0.0.1", 6379); bool b1 = redisClient. set ("testKey1", "testValue111"); bool b2 = redisClient. set ("testKey2", "testValue222", DateTime. now. addSeconds (5); // set the expiration time to 5 s string value1 = redisClient. get <string> ("testKey1"); string value2 = redisClient. get <string> ("testKey2"); Console. writeLine ("value1:" + value1 + ", value2:" + value2); Thread. sleep (6000); string value22 = redisClient. get <string> ("testKey2"); Console. writeLine ("value22:" + value22); // testKey2 expired and value22 is empty. readLine () ;}catch (Exception ex) {throw ex ;}}}

This is how to use Redis in. Net through ServiceStack.

However, the latest version of ServiceStack has been commercialized and many restrictions have been added. For example, a maximum of 6000 requests can be reached in an hour. The official instructions are as follows:

If you have a few users, you will start to charge fees !!

There is no such thing as the path to death. ServiceStack charges fees. There is no StackExchange ~

Let's learn how to use StackExchange. Redis together.

(The siege lion is a diligent and studious Spokesperson)

 

Using Redis StackExchange. Redis in. Net

Install StackExchange. Redis

Search for StackExchange. Redis and install

After the installation, open References to see if StackExchange. Redis exists.

Codoon start code

Class Program {static void Main (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"); string value = db. stringGet ("testKey1"); Console. writeLine (value); // output aaabbb} public class ClientMgr {private static readonly object locker = new object (); private static ConnectionMultiplexer client; public static ConnectionMultiplexer GetClient (string connectionStr) {if (client = null) {lock (locker) {if (client = null) {client = ConnectionMultiplexer. connect (connectionStr) ;}} return client ;}}}

Well, the basic usage of StackExchange. Redis is like this.

If you have any questions or comments, you can discuss them and make progress together ~

 

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

 

 

 

 

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.