Distributed cache design on the Net Platform

Source: Internet
Author: User

Caching is really a good thing. It can effectively improve the speed of the system in large systems. This is not much nonsense. under the. Net platform, I divided the cache function into two categories: Data Object cache and page output cache. The data Cache is implemented by the System. Web. Caching. Cache class. You can obtain the reference of this object from the Context object Context. Cache. The page/control output cache controls the cache policy based on the cache statement in the header when the. Net environment is running. This article mainly demonstrates some applications and problems related to data caching.

Some people mentioned that "data cannot be shared across Web Parks". Although the solution is to use XML files to store cached key values, there is a doubt here. net Web garden, since the process is independent, how can we share it? If so, even the objects written to the cache key value cache through XML documents cannot be shared in both processes at the same time, the benefit here is that it only avoids reading "dirty" cached data that has failed in the current process in other processes, in this way, several Web parks will generate several cached objects, and the utilization of system resources will be relatively low. if we use Web field deployment, there will be more waste. Maybe few forums have reached such a scale, so they are not within the scope of design capabilities. CommunityServer also uses this system object and encapsulates it to form the CommunityServer. Components. CSCache class, which is good and can be used in projects.

The NullBackingStore method in CacheBlock of EnterpriseLibrary should be implemented based on this class, however, to meet the requirements of multi-process/SERVER common process cache data, EntLib also provides a solution that uses SQL SERVER as the backend storage device, so that the performance requirements are not too strict, this method can also be used when there are not many client connections. you only need to configure EntLib as the working method of the shared database partition. All CacheManager instances have read and write permissions on the cache block. Of course, you can also configure to allow only one instance to write, others.

There is still no better way, but there are actually some. However, I am surprised that there is no "Original Ecology" distributed cache solution on the. Net platform. It may be a poor news. Please share what anyone knows. Fortunately, we have Memcached, which has achieved great success on the PHP platform. It is an excellent distributed cache solution. Please refer to this article, it should be necessary for large websites. if you have any questions, you can take a look. In addition, you can also think of a new idea, that is, to implement the IBackingStore interface Extension Based on EntLib and derive an implementation from BaseBackingStore, similar functions can be implemented through distributed middleware technologies such as Remoting or ICE.

Using XML as the cache key storage method is a good idea. In this way, you do not need to perform a scan when removing cache items in batches to directly obtain the corresponding cache key value, integration with distributed cache should be a good solution.

Okay, let's look back at Discuz! What are some tips for NT on page caching.

In general, I do not like the page output cache function provided by. Net2.0. It is mainly because it cannot manually control the expiration of the page cache, and it seems a little uncomfortable to have cache dependencies. In fact, the use of data-bound controls is relatively resource-consuming. I use StringBuilder to directly spell out the same data, and the output speed is much faster. The test code is relatively simple and I will not give it here, you can test it on your own, Discuz! NT has also adopted this method extensively in the design (no wonder the speed is so fast ;)). Generally, after a template is saved, a corresponding page file is generated in the aspx directory in the background. For example, if you have a page, the name of a visitor needs to be displayed on it. Its pseudo code may look like this.

Show.html:

Reference content is as follows:

<Html>

<Body>

Hello, Your name is <% yourname %>

</Body>

</Html>

Generated file show. aspx

Reference content is as follows:

TemplateBuilder. AppendLine ("

TemplateBuilder. AppendLine ("<body> ");

TemplateBuilder. AppendLine ("Hello, Your name is" + this. yourname); templateBuilder. AppendLine ("</body> ");

TemplateBuilder. AppendLine ("

Generated file show. aspx

Reference content is as follows:

TemplateBuilder. AppendLine ("");

TemplateBuilder. AppendLine ("");

TemplateBuilder. AppendLine ("Hello, Your name is" + this. yourname); templateBuilder. AppendLine ("");

TemplateBuilder. AppendLine ("");

This. yourname corresponds to an attribute in the corresponding page background class, which is initialized and assigned by the program during runtime. In this way, the final page execution result can be obtained from the ToString () of the templateBuilder object () the method is as follows: templateBuilder is an instance of the StringBuilder class in the page background class. Finally, the OnLoad event after the page is executed depends on different page types, such as homepage, channel homepage, content pages, use different cache policies to insert HTML code of page execution results into the cache, when the next request comes in, you can check whether the cache is valid in the HttpModule (which also contains the address rewriting code) before entering the page lifecycle, and directly read the cache from the memory and send it back to the client. the speed is faster, and the execution time displayed on the page is naturally 0 ms. however, because login users need to display different login information, they cannot use an anonymous cached file version. Therefore, once you log on to the page, it will be executed once, however, all the data to be displayed above has an independent cache. So it's just a re-assembly of Page code. The speed is still relatively fast. On the official forum, we can see that the homepage loading time is 15 ms, which is fast enough.

I thought that even this time could save some time. For example, the user login information can generate a piece of JS Code. When an anonymous user's cached version is sent to the browser, the system judges that such a piece of JS Code will be appended if the user logs in, replace the corresponding HTML in it, or use AJAX technology to retrieve it on the client, this solves the problem of shared cache versions between logged-on users and non-logged-on users, at least at the home page level. Other major pages should be similar, I am not very familiar with the process of Forum programs.

From another perspective, login users should not be slower than anonymous users.

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.