redis 2
Posted qinls
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 2相关的知识,希望对你有一定的参考价值。
package com.bwoil.c2b.service.redis.productRedis; import com.bwoil.c2b.cache.service.RedisService; import com.bwoil.c2b.service.product.dao.BwoilGoodsSpecIndexDao; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import java.util.Map; /** * 在 dao 层处理缓存 * Created by qinls on 2019/1/19. */ public class productRedisAspect { @Autowired private RedisService redisService; //redies保存预生成货品的key前缀 private static final String REDIS_KEY_PRE_PROD = "PROD_"; //map中存放预生成货品规格值的后缀信息 private static final String SUFIX_PRE_PROD_NAME = "_NAME"; //预生成产品在缓存中保留三十分钟 private static final long REDIS_PRE_PROD_EXPRIRES = 30 * 30 * 60; // 设置切点 更新,插入,删除 @Pointcut("(execution(* com.bwoil.c2b.service.product.dao.BwoilProductsHomeShowDao.insert*(..))) ||" + "(execution(* com.bwoil.c2b.service.product.dao.BwoilProductsHomeShowDao.delete*(..))) || " + "(execution(* com.bwoil.c2b.service.product.dao.BwoilProductsHomeShowDao.delete*(..)))") public void deleteProductBasicCache() { } //设置切点 基础维护查询 @Pointcut("(execution(* com.bwoil.c2b.service.product.dao.BwoilProductsHomeShowDao.select*(..)))") public void selectProductBasicCache() { } @Around("selectProductBasicCache()") public Object aroundSelectProductBasicCache(ProceedingJoinPoint joinPoint) { //获取目标类名,方法名 //类名(指向父类,如果使用自动生成包的mapper方法,会指向tk包下的Mapper类) String clazzName = joinPoint.getSignature().getDeclaringTypeName(); //方法名 String methodName = joinPoint.getSignature().getName(); String applId = null; // 参数 Object[] args = joinPoint.getArgs(); if (args != null && args.length > 0) { applId = String.valueOf(args[0]); } //hash field String redisKey = applId; //前置 // Object objectFromRedis = RedisOperationManager.init().getHash(clazzName, methodName + redisKey); // redisService.set(REDIS_KEY_PRE_PROD + clazzName + methodName + redisKey, toRedies, REDIS_PRE_PROD_EXPRIRES); // Map<String, Object> fromRedies = (Map<String, Object>) redisService.get(REDIS_KEY_PRE_PROD + ruleId); // redisService.remove(REDIS_KEY_PRE_PROD + ruleId); //获取从redis中查询到的对象 Map<String, Object> objectFromRedis = (Map<String, Object>) redisService.get(REDIS_KEY_PRE_PROD + clazzName + methodName + redisKey); if (null != objectFromRedis) { //System.out.println("从redis中查询到了数据...不需要查询DB,Mapper类"+clazzName+":方法"+methodName); return objectFromRedis; } //System.out.println("没有从redis中查到数据..."); Object object = null; try { object = joinPoint.proceed(); } catch (Throwable e) { throw new RuntimeException(e); } //System.out.println("从DB中查询的数据..."+object); //后置:将数据库中查询的数据放到redis中 // System.out.println("调用把DB查询的数据存储到redis中的方法...存储1小时,Mapper类+"+clazzName+":方法"+methodName); // RedisOperationManager.init().setHash(clazzName,methodName+redisKey,object,3600*24*30);//存15天 redisService.set(REDIS_KEY_PRE_PROD + clazzName + methodName + redisKey, object, REDIS_PRE_PROD_EXPRIRES); //将查询到的数据返回 return object; } }
以上是关于redis 2的主要内容,如果未能解决你的问题,请参考以下文章