[Beginner's note] Using Redis as a cache service in the. Net project,. netredis

Source: Internet
Author: User
Tags redis desktop manager redis server

[Beginner's note] Using Redis as a cache service in the. Net project,. netredis

Recently, due to project requirements, we have deployed redis in the system cache service department and finally had the opportunity to play in actual development. Before that, we had to read and write on our own, but nothing had happened, this is a summary of the system learning and practice process.

 

Basic knowledge related to Redis

Redis is an open-source distributed NoSql database that can be used for cache services, message queues, and data storage. It features a wide range of data types and high efficiency, which is almost the opposite! If you have never understood this, you can go to the Q & A client ~ The diversity of clients shows how powerful its community is:

Among them, the C # client has so many:

That's right. We also selected the most popular StackExchange. Redis as the underlying service in our project.

Redis can also be deployed on windows, but the efficiency will be greatly reduced. Therefore, Redis is usually deployed on linux and runs on the last deployment. A centos virtual machine is created in the. net core project and can be used directly. If you do not have a virtual machine, click here. There are many tutorials on the Redis service deployment network, So I skipped them here.

Here we recommend a redis graphical client Redis Studio, which is much easier to use than Redis Desktop Manager. Who knows how to use it! You can view the running status, view the data and type, view the remaining validity period, refresh the data, and delete the data. You can even configure redis directly on the graphic interface, and no longer have to go to the configuration file.

Of course, all of the above operations can be performed by using the redis-cli tool after the redis service is enabled.

Basic operation Encapsulation

The basic cache operations are nothing more than get and set, so a unified interface is defined:

Using the APIS provided by StackExchange. Redis to perform these operations, the code will not be pasted out. The connection pool is used to process redis connections. The connection object ConnectionMultiplexer is encapsulated in a blocking queue. During each read/write operation, it is retrieved from the queue and put back after use, the connection pool is initialized when the application starts.

There are two methods to create a connection. The first method is to write the required parameters in a string in the form of a connection string:

The second is to use the ConfigurationOptions object:

EndPoints is the address of the redis server. You can write multiple EndPoints when creating a cluster. In order to clarify the meaning of the parameters, the source code of StackExchange. Redis is cloned from github, which is very clear. When looking at the source code, we found that the underlying layer had recorded the detailed running status of redis, but they were all written in Stream, therefore, based on the system's log4net log, I wrote a set of TextWriter based on its implementation to achieve log persistence, which is used for subsequent analysis errors:

I encountered a problem when encapsulating generic operations: I want to save a complex object. The first method was serialization, but I always felt that this would drag down the performance of redis and I felt uncomfortable and didn't want to use it. The hash type of redis is used, but the operation is very inconvenient, and there is no way to store the set or pass it. If other types are converted to the source code, only int, string, and bool are supported, no. There is no way to serialize it. Then I think of the protobuf recommended by Dr. Zhang Shanyou, saying that the performance is superb. So I used it and found that it was converted using Stream. Isn't that the reason for its high performance? Which of the following experts will give you some advice ~

Use redis to customize session Storage

After the cache service is set up, we plan to move the session to it to facilitate distributed and unified State management. It's easy to rewrite a set of SessionStateStoreProviderBase, and then follow the yundun blog in the garden, "Session final in Distributed Redis". However, it cannot be run, it should be related to my encapsulation. Due to time issues, I put it down first. I directly went to nuget to find a ready-made product. I took a fancy to Microsoft. web. redisSessionStateProvider, thinking that since it is my soft official should be no big problem, and this SessionStateProvider is also dependent on StackExchange. redis, consistent with the project, was decisively downloaded and installed. Next, I had to boast about Microsoft's detailed processing. After the download, I immediately found a readme and told me the installation result:

Otherwise, you need to study how to use this item and follow the prompts to open the web. in <system. web> the node automatically adds a node sessionState to define the session-related configuration. The most important thing is that the configuration template is provided, even data types are clearly marked:

As we all know, Microsoft has always admired silly operations for its developers, but these details are really very considerate. It is okay to configure key information based on your redis server information. Write a session and test it. When the page is up and running, the session value (encoded) is also found in redis. It's so cool ~ 100 uppercase likes...

 

Finally, replace all the temporary HttpContext. Cache and static Dictionary in the system with redis, and then build, run, and OK.

Summary

After several days of practice, we have opened the door to redis and stepped out of the first step. In the future, we will certainly encounter many problems in system development and operation, it doesn't mean that the program uses redis to implement get and set. Now we are only familiar with the most basic things. We also need to learn the advanced usage of redis later, for example, pub/sub, master/slave, and cluster.

Problem

1. Is there a better solution to serialization? In addition, if you use protobuf, You need to tag the class name and attribute. This is a bit of a worry ~

2. Some people say that putting the session in redis can solve the problem of session blocking. It seems that it will not work after testing. If any great God knows the truth, please give me some advice ~

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.