I recently read about discuz written by Dai zhenjun! An article on the Cache Design of NT Article Discuz! NT Cache Design Analysis [original], there are some ideas, let's write it here for everyone to make a brick .;)
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. for data caching, the system. web. caching. the cache class can be implemented from the context object context. cache to obtain the reference of this object. the page/control output cache is composed.. NET environment control the cache policy based on the cache statement in the header during runtime. this article mainly demonstrates some applications and problems related to data caching.
The Chinese representative mentioned "the problem of data sharing across web Parks". Although the solution was 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 a communityserver. components. cscache is a good class 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 under the. NET platform. Maybe it's just a bit boring. If anyone knows this, please share it. 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 in page cache.
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, and the same data I use stringbuilder to directly spell out the output speed is much faster, testingCodeI won't 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 pseudocode may look like this.
Show.html:
<HTML>
<Body>
Hello, your name is <% yourname %>
</Body>
</Html>
Generated file show. aspx
Templatebuilder. appendline ("<HTML> ");
Templatebuilder. appendline ("<body> ");
Templatebuilder. appendline ("Hello, your name is" + this. yourname );
Templatebuilder. appendline ("</body> ");
Templatebuilder. appendline ("
This. yourname corresponds to an attribute in the background class of the corresponding page. Program Initialize and assign values during running, so that the final page execution result can be obtained from the tostring () method of the templatebuilder object, templatebuilder is an instance of the stringbuilder class in the page background class. The onload event after the page is executed depends on different page types, such as homepage, channel homepage, and content page, use different cache policies to insert the HTML code of the page execution results into the cache. When the next request comes in, the httpmodule before the page lifecycle (this also includes the address rewriting Function Code) to determine whether the cache is valid, directly read from the memory cache 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, the data to be displayed above all has independent cache items, so it is only necessary to re-assemble the Page code. The speed is still relatively fast. On the official forum, we can see that the homepage loading time is 15 ms, 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 ~~~;)
Hoho: It's finished. Write it first. Other things '~~ In general, I think it is best to be practical. Don't say how bad DNT design is, how unreasonable the hierarchy is, and how code is not oo, however, in such a specific application environment, it provides a relatively flexible and fast solution, which I think is a correct choice. Although the maintenance and reusability of the things made by tailor-made products are poor, it still has its advantages. I also prefer this highly targeted solution, hoping that DNT can do better ~~~ For a program like blogengine, it almost loads a lot of data into the memory. By default, data storage is also stored in XML. It is hard to say that it is a bad program, because it has specific application scenarios, the best is the best ~~~~
this article is based on DNT 2.0 RC1. If not, please make an axe. Thank you.