Server-side Join cache 1. Background
Because the project progress is more tight. And the first time the architecture of their own design system, just started to consider not completely, mainly thinking of the system to achieve the function of the first. So when we first started designing the system, we didn't think about caching. But for a web system already. The cache not only can greatly reduce the pressure of the database, but also can improve the response time of the system to a great extent. Now that the system is almost identical in function, it is now necessary to add a cache to the system. But because the system function is almost the same as finished. The code is very much written, so it's really harder to join the cache today.
Let's talk about the overall framework of our current system.
It is clear that the architecture we are using now is simpler, and all requests are .htaccess
forwarded to the file index.php
and then the index.php
forwarding function is started in the file. The request is assigned to a specific ' Server ' run-specific by the request Action
. In this case, Action
a service is called to retrieve the data from the database and return it to the client. The following is the class diagram for the current system (in order to show the method being called. So a little nonstandard).
2. Adding a cache
There are many ways to add a cache, and the most important thing to think about is to join directly in the dao
layer. Before data is taken from the database. First from the cache, assuming that the cache does not have to be taken from the database, and then put in the cache. Another strategy is to add a service
cache to the layer. It is also said to be dao
put into the cache after assembling the extracted data from the layer.
First analyze the pros and cons of both scenarios:
The first scenario:
Advantage: First in the dao
layer cache for each sql
run results cache, cache granularity is smaller, cache hit rate will be higher.
Cons: Each sql
query data is different. may have been the same information, but probably more than one field is indeed a cache record, this is a very large waste, after all, the cost of caching is still relatively high. Second, assuming that caching is done directly at the dao
layer, some places do not actually want to cache, then it is more inconvenient to control.
Another option:
Advantage: In the service
cache, the first is the original system based on the addition of less code. Second, the assembled data cache because service
of the general changes in the number of small. The cached data will be less, and the hit rate will be good.
Cons: The cache size is larger and the hit rate is not so ideal. And the reuse of the cache is also worse.
Synthesizing the above analysis basically can know whether the dao
cache at layer or service
layer has both advantages and disadvantages. Finally, consider combining the advantages of both. Abstracts out a cache layer. Controls whether the cache is cached in the cache layer. As well as caching logic, the same time the cached object is changed to a database corresponding to an object, so that the reuse of cached objects and hit rate has a very good effect.
The following is the class diagram after the cache is added:
In the original framework on the basis of a service
direct call cacheServer
to load the specified cache
, the original logic does service
not change, but called dao
the original call cache
can be called instead.
To this cache basically has been added, the code is finished, the use of the current feeling can be, and inappropriate places to hope that readers can communicate more.
Finally thanked Http://www.crackedzone.com/server-need-a-new-datastore-layar.html article blogger, has given me very to the revelation.
To add a cache to an application