Why is the cache added to the service layer instead of the controller layer?
All operations to access the database must go through the service layer, if the data in Controllera cache, when controllerb to the database to increase, delete, change operation, Controllerb can not know controllera how to cache the data, It is not possible to update the data in the Controllera cache, at which point the Controllera is storing dirty data.
If the cache is added to the service layer, because all accesses to the database must go through the service layer, we can intercept at the service layer: If the data is queried, the data is stored to the cache according to the rules, and if the data is added, deleted or changed, the data in the cache is purged according to the rules.
How do I ensure that the data in the cache is up to date? if it is a query operation, then use the Cacheadd method to intercept, the operation is as follows:using System name + class name as key, system name + class Names + method name + parameter name as value stored in the cache, and then the system name + class name + method name + parameter name as key, the data queried as value stored to the cache. if the increment, delete, change the operation, then use the Cachedelete method to intercept, the operation is as follows:according to the system name + class names as key, take out the corresponding value value ( system name + class name + method name + parameter name ), and then remove the system name + class name + method name + parameter name as key, and then delete the corresponding value value. How do I configure Redis? Add the following dependencies to the core and ear pom files
<dependency> <groupId>com.tgb</groupId> <artifactid>ejbredis-cache</ artifactid> <version>${project.version}</version> </dependency> < dependency> <groupId>org.codehaus.jackson</groupId> <artifactId> jackson-mapper-asl</artifactid> <version>1.9.13</version> </dependency>
Add the following configuration file to the core resources Cache.xml is configured with Redis service information:
<?xml version= "1.0" encoding= "UTF-8"?><redis-server>
Add the following annotations to the methods that need to be intercepted In this regard, the realization of Redis in the Java-itoo and implementation of the way has been explained, hope can help you grow and progress.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The realization of Redis in Java-itoo