The realization of Redis in Java-itoo

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.