spring整合redisson开启缓存方式
目录
- spring整合redisson开启缓存
- @Cacheable
- @CachePut
- @CacheEvict
- spring集成redisson踩过的坑
spring整合redisson开启缓存
先来了解几个注解:
@Cacheable
表明所修饰的方法是可以缓存的:当第一次调用这个方法时,它的结果会被缓存下来,在缓存的有效时间内,以后访问这个方法都直接返回缓存结果,不再执行方法中的代码段。
这个注解可以用condition属性来设置条件,如果不满足条件,就不使用缓存能力,直接执行方法。
可以使用key属性来指定key的生成规则。
支持如下几个参数:
value
:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name, 指明将值缓存到哪个Cache中key
:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL,如果要引用参数值使用井号加参数名,如:#userId
@CachePut
与@Cacheable不同,@CachePut不仅会缓存方法的结果,还会执行方法的代码段。它支持的属性和用法都与@Cacheable一致。
@CacheEvict
与@Cacheable功能相反,@CacheEvict表明所修饰的方法是用来删除失效或无用的缓存数据。
有如下几个参数:
value
:缓存位置名称,不能为空,同上(可以理解成缓存得分区)key
:缓存的key,默认为空,同上condition
:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpELallEntries
:true表示清除value中的全部缓存,默认为falsebeforeInvocation
:方法执行前就清楚缓存,不管方法是否报错
springboot配置加
spring.cache.type=redis
在启动类开启缓存
设置缓存超时时间,单位毫秒
spring.cache.redis.time-to-live:1000
spring集成redisson踩过的坑
我用spring的xml集成一直报错,所以只能选择注解方式:
@Configuration public class RedissionConfig { Logger log = LoggerFactory.getLogger(RedissionConfig.class); @Value("${redis_ip}") String redis_ip; @Value("${redis_port}") String redis_port; @Value("${redis_password}") String redis_password; @Bean(name="redissonClient") public RedissonClient init(){ log.info("redis_ip:"+redis_ip); log.info("redis_port:" +redis_port); log.info("redis_password:"+redis_password); Config config = new Config(); String url = "redis://"+redis_ip+":"+redis_port; log.info(url); config.useSingleServer().setAddress(url).setPassword(redis_password); RedissonClient redissonClient = Redisson.create(config); log.info("初始化RedissonClient"); return redissonClient; } }
第一坑就是版本兼容问题,我用的Spring是4.2.7,第一次集成的是3.12.0,会报以下错误:
严重: Unable to process Jar entry [module-info.class] from Jar [jar:file:/C:/Users/Administrator/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.10.1/jackson-dataformat-yaml-2.10.1.jar!/] for annotations
org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:133)
at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:60)
at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:209)
at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:119)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2134)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2010)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1976)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1961)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1319)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
所以把版本降低到3.10.0,就不会报错了。
第二个坑是设置密码问题,因为要区分多环境,所以把redis的信息写到配置文件中,如果按照上面的配置,会报以下错误:
Caused by: org.redisson.client.RedisException: ERR Client sent AUTH, but no password is set. channel: [id: 0xe37c85e0, L:/192.168.0.128:54867 - R:192.168.0.128/192.168.0.128:5802] command: (AUTH), params: []
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:314)
at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:130)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:110)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:502)
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:278)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:656)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:591)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:508)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:470)
... 3 more
所以要判断密码是否为空,如果是空不做参数即可,下面是改正过的代码:
public RedissonClient init(){ log.info("redis_ip:"+redis_ip); log.info("redis_port:" +redis_port); log.info("redis_password:"+redis_password); Config config = new Config(); String url = "redis://"+redis_ip+":"+redis_port; log.info(url); SingleServerConfig singleServerConfig = config.useSingleServer().setAddress(url); if(!StringUtils.isEmpty(redis_password)){ singleServerConfig.setPassword(redis_password); } RedissonClient redissonClient = Redisson.create(config); log.info("初始化RedissonClient"); return redissonClient; }
另外,网上的帖子好多直接放的ip+端口号,但是我本地会报错,一定在加redis://192.168.2.128:6379这种格式的,可能是因为版本问题,也有可能就是误导人的。
看到上面的报错,感觉知道xml的错误了:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:redisson="http://redisson.org/schema/redisson" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd"> <beans profile="development"> <!--redisson --> <!--<redisson:client id="redissonClient"> <redisson:single-server address="redis://182.92.97.141:5802"/> </redisson:client>--> </beans>
没有设置密码,一定不要把password属性放上去,要不会报错的。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。
【文章出处:防御服务器 】