(github address: https://github.com/jiangcaijun/ssm)
2017-05-28
For some requirements, such as items that only need to be queried, or some data only need to be queried, without adding or deleting items (such as provincial and urban tables), aop and redis can be used to query data first from redis, if not, then to the database to query, and then to put the data queried in the database into redis, the next query can be directly from redis. As you can see, there is no need to query the database.
1. Integration of spring and redis
Reference link:
2. redis section
Redis.java: Define Redis annotations, Target for classes and methods
package com.ssm.annotation; import java.lang.annotation.*; @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Redis { }
3,redisAspect.java
Aspect: Query redis before querying. If the query does not penetrate the database, save redis after querying the data from the database. Then the next query can hit the cache directly.
@Aspect @Component public class RedisAspect extends BaseController{ private static final Logger logger = LoggerFactory.getLogger(RedisAspect.class); @Autowired private RedisUtil redisUtil; // Controller Layer Tangent Point @Pointcut("@annotation(com.ssm.annotation.Redis)") //Annotation is used to match the method currently executing the method with the specified annotation; public void redisAspect() { } @Around("redisAspect()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { //Obtaining Target Method Parameters Object[] args = joinPoint.getArgs(); String applId = null; if (args != null && args.length > 0) { applId = String.valueOf(args[0]); } // Request class name String targetName = joinPoint.getTarget().getClass().getName(); // Request method String methodName = joinPoint.getSignature().getName(); //key format in redis: request class name + request method + target method parameter String redisKey = targetName + methodName + applId; logger.debug("Call from redis The method of inquiry in. redisKey=" + redisKey); //Get the object queried from redis Object objectFromRedis = redisUtil.getData4Object2Redis(redisKey); //If queried if(null != objectFromRedis){ logger.debug("from redis The data was queried....No need to query the database"); return objectFromRedis; } logger.debug("Not from redis Data found in..."); //If not, then query the database Object object = null; object = joinPoint.proceed(); //Postposition: Put the data queried in the database into redis logger.debug("Store database query data into redis Method in..."); redisUtil.setData4Object2Redis(redisKey, object); //Return the queried data return object; } }
4. spring-mvc.xml: Configure XML
Here the class is the path to the RedisAspect.java
<!-- redis Cache annotation --> <bean id="RedisAspect" class="com.ssm.annotation.RedisAspect"/>
5. controller layer
Add the @Redis annotation to the method requiring redis cache processing, as follows:
@Redis @RequestMapping(value = "/testAjax", method = RequestMethod.POST) @ResponseBody public Object test(Model model) { JSONObject jsonObject = new JSONObject(); jsonObject.put("id","The data is available."); return jsonObject.toJSONString(); }
Project Start: First run to this method, background log, as follows:
2017-05-24 13:58:10 [com.ssm.annotation.RedisAspect.around (RedisAspect.java:46)] - [DEBUG] Calls the method to query from redis, redisKey = com.ssm.controller.UserController test {} 2017-05-24 13:58:10 [com.ssm.annotation.RedisAspect.around (RedisAspect.java:56)] - [DEBUG] did not find data from redis. 2017-05-24 13:58:10 [com.ssm.annotation.RedisAspect.around (RedisAspect.java:63)] - [DEBUG] Method of storing database query data into redis...
Browser refresh page, console output, as follows:
2017-05-24 13:58:19 [com.ssm.annotation.RedisAspect.around (RedisAspect.java:46)] - [DEBUG] Calls the method queried from redis, redisKey = com.ssm.controller.UserController test {} 2017-05-24 13:58:19 [com.ssm.annotation.RedisAspect.around (RedisAspect.java:52)] - [DEBUG] queries data from redis... no database queries are required