1. SpringCache实战遇坑

1.1. pom

  1. 主要是以下两个
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 配合redis做缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

1.2. Redis配置

package com.zhiyis.common.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import redis.clients.jedis.JedisPoolConfig; import java.lang.reflect.Method; @Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport { private static Logger logger = LoggerFactory.getLogger(RedisConfig.class); @Value("${spring.redis.host}")
private String redisHost; @Value("${spring.redis.port}")
private int redisPort; @Value("${spring.redis.timeout}")
private int redisTimeout; @Value("${spring.redis.password}")
private String redisAuth; @Value("${spring.redis.database}")
private int redisDb; @Value("${spring.redis.pool.max-active}")
private int maxActive; @Value("${spring.redis.pool.max-wait}")
private int maxWait; @Value("${spring.redis.pool.max-idle}")
private int maxIdle; @Value("${spring.redis.pool.min-idle}")
private int minIdle; @Bean
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
} @Bean
public CacheManager redisCacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
//默认300秒过期
cacheManager.setDefaultExpiration(300);
// 启动时加载远程缓存
cacheManager.setLoadRemoteCachesOnStartup(true);
//是否使用前缀生成器
cacheManager.setUsePrefix(true);
return cacheManager;
} @Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxWaitMillis(maxWait);
poolConfig.setMinIdle(minIdle);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(false);
poolConfig.setTestWhileIdle(true);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
jedisConnectionFactory.setPassword(redisAuth);
jedisConnectionFactory.setHostName(redisHost);
jedisConnectionFactory.setDatabase(redisDb);
jedisConnectionFactory.setPort(redisPort);
jedisConnectionFactory.setTimeout(redisTimeout);
return jedisConnectionFactory;
} @Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
Jackson2JsonRedisSerializer<Object> serializer = jackson2JsonRedisSerializer();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(serializer);
return redisTemplate;
} @Bean
public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
final Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
final ObjectMapper objectMapper = Jackson2ObjectMapperBuilder
.json().build();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
return jackson2JsonRedisSerializer;
}
}

在application.properties填上相应的参数

1.3. 使用

1.3.1. 坑1

  1. 目前主要使用的就是缓存和删除缓存
@Cacheable(sync = true, value = "on_hospital_list", key = "'3003101006_'+#requestReport.body['carer_id']", condition = "#requestReport.body['carer_id'] !=  '' ")
@Override
public ResponseReport getHospitalList(RequestReport requestReport) {
ResponseReport responseReport = new ResponseReport();
。。。
return responseReport.returnSuccessResult(hospitals, "获取医院列表成功", requestReport);
}
  1. 这里没有经验的人可能会纠结很久,因为我封装的入参对象,里面放的是JSONObject或者map作为的body值,这里我一开始是写成requestReport.body.carer_id这样的,但是这样会报如下错误
EL1008E: object of type 'com.alibaba.fastjson.JSONObject' - maybe not public

但你在网上找答案,都是文不对题,或者说其他错误导致相同的报错,反正我是找不到正确的解答

3. 解决方法就是如上代码,直接写成#requestReport.body['carer_id']

1.3.2. 坑2

  1. 删除缓存,我自定义了一个注解,原因是好像CacheEvict没提供删除多个key的方法
//        @CacheEvict(value = "on_hospital_list", key="'3003101006_'+#requestReport.body['carer_id']")
@CacheRemove(value = "on_hospital_list"/*,key={"'3003101006_'+#requestReport.body['carer_id']","'3003101007_'+#requestReport.body['carer_id']"}*/)
@Override
public ResponseReport upDownServer(RequestReport requestReport) {
。。。业务逻辑 return responseReport.returnError("9999", "上下线失败", requestReport);
}
  1. 注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheRemove { /**
* 需要清除的大类 例如 autocms 所有缓存
*
* @return
*/
String value() default ""; /**
* 需要清除的具体的额类型
*
* @return
*/
String[] key() default {};
}
  1. 注解实现

import com.zhiyis.framework.annotation.CacheRemove;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component; import java.lang.reflect.Method; /**
* 清除缓存切面类
*
* @author laoliangliang
* @date 2019/1/14 16:04
*/
@Component
@Aspect
public class CacheRemoveAspect { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
RedisTemplate<String, String> redis; ExpressionParser parser = new SpelExpressionParser();
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); /**
* 截获标有@CacheRemove的方法
*/
@Pointcut(value = "(execution(* *.*(..)) && @annotation(com.zhiyis.framework.annotation.CacheRemove))")
private void pointcut() {
} /**
* 功能描述: 切面在截获方法返回值之后
*/
@AfterReturning(value = "pointcut()")
private void process(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
//获取切入方法的数据
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入方法
Method method = signature.getMethod();
//获得注解
CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class); //注解解析
String[] params = discoverer.getParameterNames(method);
EvaluationContext context = new StandardEvaluationContext();
for (int len = 0; len < params.length; len++) {
context.setVariable(params[len], args[len]);
} if (cacheRemove != null) {
StringBuilder sb = new StringBuilder();
String value = cacheRemove.value();
if (!value.equals("")) {
sb.append(value);
}
//需要移除的正则key
String[] keys = cacheRemove.key();
sb.append(":");
for (String key : keys) {
Expression expression = parser.parseExpression(key);
String value1 = expression.getValue(context, String.class);
//指定清除的key的缓存
cleanRedisCache(sb.toString() + value1);
}
} } private void cleanRedisCache(String key) {
if (key != null) {
//删除缓存
redis.delete(key);
logger.info("清除 " + key + " 缓存");
}
}
}
  1. 这里的注解写入参数,如果想要使用spel表达式,要写上解析注解的一段代码

参考

https://huan1993.iteye.com/blog/2395239

http://www.cnblogs.com/imyijie/p/6518547.html

SpringCache实战遇坑的更多相关文章

  1. redis 集群 遇坑1

    redis 集群 遇坑1 redis集群需要开2个端口 一个是客户端连接端口 一个是 集群总线端口 集群总线端口 是 客户端端口 + 10000 如 客户端端口是 6380 则集群总线端口 为 163 ...

  2. rabbitmq在ios中实战采坑

    1. rabbitmq在ios中实战采坑 1.1. 问题 ios使用rabbitmq连接,没过多久就断开,并报错.且用android做相同的步骤并不会报错,错误如下 Received connecti ...

  3. 浅谈Android Studio3.0更新之路(遇坑必入)

    >可以参考官网设置-> 1 2 >> Fantasy_Lin_网友评论原文地址是:简书24K纯帅豆写的我也更新一下出处[删除]Fa 转自脚本之家 浅谈Android Studi ...

  4. Android 上传开源项目到 jcenter 实战踩坑之路

    本文微信公众号「AndroidTraveler」首发. 背景 其实 Android 上传开源项目到 jcenter 并不是一件新鲜事,网上也有很多文章. 包括我本人在将开源项目上传到 jcenter ...

  5. Cat搭建遇坑记

    1. Cat搭建遇坑记 1.1. 报错 服务端启动 Unable to get component: class com.dianping.cat.analysis.TcpSocketReceiver ...

  6. deno+mongo实战踩坑记

    自从 deno 1.0 发布以来,有关 deno 的文章很多,大多数都是在讨论怎么安装 deno .deno 有哪些特点 .deno 和 node 有哪些异同.deno是不是 node 的替代品等.咱 ...

  7. Windows安装Scrapy遇坑解决办

    PS: Windows真心不适合开发.且行且珍惜.... 坑: error: Setup script exited with error: Microsoft Visual C++ 9.0 is r ...

  8. Java之戳中痛点 - (6)避免类型自动转换,例如两个整数相除得浮点数遇坑

    先来看一个例子: package com.test; public class calculate { /** * 光速30万公里/秒 */ public static final int LIGHT ...

  9. Android组件化demo实现以及遇坑分享

    首先贴出demo的github地址:GitHub - TenzLiu/TenzModuleDemo: android组件化demo 作者:TenzLiu原文链接:https://www.jianshu ...

随机推荐

  1. Devexpress使用之:GridControl控件

    Devexpress使用之:GridControl控件 Devexpress系列控件功能很强大,使用起来也不太容易,我也是边摸索边使用,如果有时间我会把常用控件的使用方法整理出来的. using Sy ...

  2. ----改写superheros的json以及上传到github----

    以下为js代码: var header = document.querySelector('header'); var section = document.querySelector('sectio ...

  3. System.Web.Caching.Cache缓存帮助类

    /// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...

  4. flag:用心学习的第一天

    目标是:加油学习,尽早改变世界

  5. 软件测试---测试模型(V、W、H)

    一.V测试模型 1.V模型示意图: 单元测试:又叫模块测试,针对软件设计中的最小单位—>程序模块 集成测试:又叫组装测试,通常在单元测试的基础上,将所有程序模块进行有序.递增测试. 系统测试:把 ...

  6. Spring学习-01

    一.Srping 一个轻量级DI.IOC.AOP的容器框架 DI:依赖注入 IOC:控制反转 AOP:面向切面 二.构造器注入 Constructor-arg 属性:index/name/type/r ...

  7. WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@584] - Cannot open channel to 4 at election address Slave3.Hadoop/xxx.xxx.xxx.xxx

    这些日子为这个错误苦恼很久了,网上找到的各种方法都试了一遍,还是没能解决. 安装好zookeeper后,运行zkServer.sh start 显示正常启动,但运行zkServer.sh status ...

  8. 在windows上传一个新的项目到GitHub上

    不多说,直接上步骤 1,新建GitHub的账号密码. 2,新建一个项目  点击new repository 3,选择自己项目,填写格式 点击创建  create  repository,这时候一个雏形 ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  10. 解决YUM下Loaded plugins: fastestmirror Determining fastest mirrors 的问题

    Centons下 其大概意思是fastestmirror不能使用,fastestmirror是yum的一个加速插件,具体我也没有仔细了解过,可能是系统不支持或者缺少组建导致的.处理办法就是禁用这个插件 ...