I recently took some time to look at it.OrchardCMS cache mechanism. I have a habit. Before reading something, I will go to Google to get some information, so I can learn it myself. I also want to see if someone pays attention to a technical point or a project like me.
I found an articleArticleThe author analyzed orchard's caching mechanism from start to end, and it seems that he has written this content by himself. Article link:
My Microsoft Open-Source CMS-orchard Source Code read orchard. caching
So if you wantCodeFor more information, see his article. This is really a good introduction. Every interface to class has a very good analysis... I will not focus too much on the interpretation of the entire code here. First, let's talk about why orchard CMS cache makes me feel very good.
First of all, from the perspective of a large entire project, because it is completely enterprise-level applications, the entire design is very complicated. This results in the reading and writing of a small amount of data to be somewhat slow. This requires a good cache mechanism.
The advantages of orchard CMS cache are summarized as follows:
1. Discard the traditional get set method.
2. Powerful cache expiration mechanism.
3. Elegant code style
4. Multi-Level Cache Control
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------- O (∩ _ ∩) O \ (^ o ^ )/~ (⊙ O ⊙ )---------------------------------------------------------------------------
If you do not know orchard CMS, try to go to the link above to learn more ....
If you know about it and are even interested in it, but haven't tried to develop a module by yourself, I will give you a cuid example below for your reference.
If you know more about module development, skip the module development section below and check the cache section below for details...
Product module development:
Orchard's module is very simple and even provides a template generation tool. Step by step, I will introduce how to create a module for adding, deleting, modifying, and querying a product.
1. Create a product project template.
First confirm that you have installed the code generation module.
Next, use the codegen command line to create a project for the product:
You can find orchard.exe in the bindirectory and use it to create a project using the command line.
Modify Your module.txt:
My product class is as follows:
Create a class in the model folder as follows:
After the project is compiled, we will add the data migration:
The migration. CS file is automatically created:
Orchard's migration is also cool. If you need to modify the data structure, you only need to add the method update1 () under this class and return 2; then when you run the management page again, there is a prompt that your module needs to be updated.
We will not introduce the Migration Mechanism too much here, or we should first return to the cache.
According to the official website, here we need to create a handler class, which is similar to the filter in Asp.net MVC. A simple class:
Next, you need to note that we need to create a service class to use the orchard cache mechanism:
The content in the red box uses the orchard cache mechanism, and the content is included in the content. product is the cache key, and the subsequent Lamda expression contains a monitor, which will monitor whether there is a Nicolas. product. if the changed event occurs for a long time and the remaining code is executed, the database will be queried. If the changed event does not occur, the product information will be retrieved from the cache.
You may have doubts, but how can you know about changed? You can read the link I provided above. The author gives a detailed introduction to the running mechanism of real code. In orchard, the driver provides data directly to the view part.
Therefore, we need driver to call the service class.
Here, you can see that I set out the changed event. At this time, when I call the service data again, the cache has expired, you need to use productrepository to retrieve data from the database.
Why is it triggered here? This is because it is the data driver of the editing page. When you edit a product and click the Save button, it means that the database data is updated, so we need to clear the cache.
The rest is the view part ,:
After compilation, go to the module Management page to make this module take effect.
For specific usage, you need to see how to use the content part of orchard CMS. We recommend that you go to the official website. I won't be jealous.
2. Analyze the above example
A. cache Implementation Method
Traditional or general cache methods are like the following:
VaR Product = (product) cahce ["product"];If(Product =Null) Product = ...; Cache. Add ("product", product ,...); In orchard, you do not need to determine whether the product exists or not. You only need to use the Lamda expression to set the cached adaptation event, that is, the when ();;;;
B. Analysis of cache expiration Mechanism
In the above example, I used when ("something_changed") to implement cache expiration settings. In fact, the cache mechanism of orchard CMS can also be controlled by time.
There is an iclock service in the orchard service.
You can use it to control orchard cache expiration .. Because all events that can trigger the orchard cache expiration event must inherit the ivlolatiletoken interface, that is, the expired token.
Example of using time to control cache Expiration:
You can also set timespan to minute or hour ....
3. Code implementation:
If you read the orchard cache implementation code, you will find that the code is really beautiful, and the entire mode and encoding are worth learning. After all, it was developed by Microsoft cool people.
4. Multi-level cache expiration control:
For operations related to some associated tables, you can store the cache in the same thread in Orchard CMS. When the cache of a sub-Table fails, the cache mechanism will automatically fail the cache of other associated tables.
PS: Recently, a friend from the garden sent a message saying that I haven't updated my blog for a long time, so I spent more than 5.1 writing a few articles, and most of the time I 've been talking about is nonsense. Work ..