所有访问数据库的操作必须经过service层,如果数据在controllerA进行缓存,当controllerB对数据库进行增、删、改操作后,controllerB无法知道controllerA如何缓存的数据,因而无法将controllerA缓存中的数据更新,此时controllerA存储的就是脏数据。
如果把缓存加在service层,因为所有访问数据库的操作都必须经过service层, 所以我们可以在service层进行拦截:如果是查询数据,则根据规则把数据存储到缓存;如果是增、删、改数据,则根据规则把缓存中的数据清除。
<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>
<?xml version="1.0" encoding="UTF-8"?> <redis-server> <host>192.168.24.46</host> <port>6379</port> <expire>0</expire> <timeout>0</timeout> <password></password> <prefixion>Basic</prefixion> </redis-server>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/quwenzhe/article/details/47167557