Novice summary using Redis as the caching service in. NET projects

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

Recently due to the project needs, in the system cache service part of the Redis, finally have the opportunity to play in the actual development, before all are their own casual look at writing, very fragmented also did not precipitate down what, this is a systematic study and practice process summary.

Basic knowledge of Redis

Redis is an open-source distributed NoSQL database that can be used for caching services, Message Queuing, data storage and so on, the data type is rich, the efficiency is high, simply inverse day! Have not learned can go to ask degrees Niang ~ Client's rich, enough to see how strong its community:

There are so many clients in C #:

Yes, our project also chose the hottest stackexchange.redis as the bottom service.

Although Redis can also be deployed on Windows, it is inefficient, so it is usually deployed on Linux, just as I created a centos virtual machine when I last deployed the. NET core project, which can be used directly, and the students who do not match the virtual machine can click here. There are many tutorials on the Redis Service deployment web, which is skipped.

Here we recommend a Redis graphical client Redis Studio, which is much more useful than Redis Desktop Manager and who knows! You can view the run, view the data and type, view the remaining time, refresh the data, delete the data, and even configure Redis directly in the graphical interface, without having to go into the configuration file.

Of course, the above can be done with the REDIS-CLI tool after the Redis service is open.

Basic Operation Package

The underlying operation of the cache is nothing more than get, set these, so the unified definition of an interface:

It uses the API provided by Stackexchange.redis to implement these operations, and the code is too much to be posted. One of the Redis connection to do a connection pool processing, the connection object connectionmultiplexer encapsulated in a blocking queue, each read and write operations to go to the queue to take, run out and put back, when the application starts will initialize the connection pool.

There are two ways to create a connection, and the first is to use the form of a connection string to write the required parameters in a string:

The second is the use of the Configurationoptions object:

Where endpoints is the address of the Redis server, you can write multiple when doing the cluster. In order to understand the meaning of the parameters inside, from GitHub clone a copy of the source of Stackexchange.redis, very clear. In the process of looking at the source code found that there are records of Redis detailed operation, but are written in the stream, so that their own system based on the log4net log and then rewrite a set of TextWriter according to its implementation to achieve the log persistence, for the day later analysis error:

There is a problem when doing generic operation encapsulation: I want to save a complex object as a whole. The first idea is to serialize, but always feel that doing so will slow down the performance of Redis, feel uncomfortable not to use. Then is the use of Redis hash type, but the operation is very inconvenient, and there is no way to save the collection, also pass, the other type turned the source to see only support int, string, bool these, also not. There is no way to serialize it, and then think of Zhang Shanyu teacher recommended Protobuf, said the performance is super-good, so use, found that it is using stream to convert, is this the reason why it is high performance? Who is the great God pointing down ~

customizing session storage with Redis

After the cache service is set up, it intends to move the session inside to facilitate distributed and unified state management. Very simple, rewrite a set of sessionstatestoreproviderbase on the line, and then follow the garden in the wake of the blog "distributed Redis Implementation session End" To do a set, but found that can not run up, should be related to my package, Because of the time problem first put down, directly on the NuGet find a ready-made, Fancy Microsoft.Web.RedisSessionStateProvider, think that since it is my soft official out of should have no big problem, in addition this sessionstateprovider also relies on Stackexchange.redis, and the project consistent so Decisive download installed. Next I have to boast about the details of Microsoft, ah, download immediately after a readme to tell me the results of the installation:

Or else to study how this thing is used, follow the prompts inside to open the Web. config, found in the <system.web> node to help us automatically added a node sessionstate, which defines the session-related configuration, The most important thing is that the configuration templates are given, and even the data types are marked clearly:

It is well known that Microsoft has always been a fool-like operation for his developers, but these details are so thoughtful that it is OK to configure key information based on your Redis server information. Write a session test, the page ran up Redis inside also found the session value (was coded), too TM cool AH ~100 a capital of praise ...

Finally, all the temporary Httpcontext.cache and static dictionary in the system are replaced with Redis, then build, run, OK.

Summarize

After these days of practice is to open the door of Redis out of the first step, the future development and operation of the system will certainly encounter a lot of problems, not to say in the program with Redis implementation get, set is learned, now contact is only the most basic things, back also learn the advanced usage of Redis , such as Pub/sub, Master/slave, clusters, and so on.

Problem

1, serialization there is still a stem in the heart, there is no better solution? And with protobuf words to tag on the class name and attributes, this is a little worry Mulberry Ah ~

2, some people say that the session to the Redis can solve the problem of blocking the session, testing a bit like not ah, which God knows the truth of the Also please pointing twos ~

Note: After the article released many friends want code, the front left the mailbox has been sent to everyone, now share to Baidu Cloud, we can go to download Http://yun.baidu.com/s/1hrVIHyG

Novice summary using Redis as the caching service in. NET projects

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.