我是靠谱客的博主 包容秀发,这篇文章主要介绍java redis 常见问题,现在分享给大家,希望可以做个参考。

1.描述:org.springframework.util.Assert.isTrue(ZLjava/util/function/Supplier;)V

   原因:redis与spring版本不匹配

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.12.RELEASE</version> </dependency>

解决:

方法1:提高Spring版本,升到spring 5.0以上(<spring-version>5.2.4.RELEASE</spring-version>)

方法2:降低spring-data-redis版本,可以改为2.0以下(1.7.2.RELEASE)

但是 redis 2.1以上的版本与 1.7的版本在setIfAbsent 方法上有区别,如下

复制代码
1
2
3
4
// 1.7版本的redis不能使用setIfAbsent方法设置过期时间 Boolean b = redisTemplate.opsForValue().setIfAbsent("lock_name", LOCK_NAME); // 2.1版本的redis可以使用setIfAbsent方法设置过期时间 Boolean b = redisTemplate.opsForValue().setIfAbsent("lock_name", LOCK_NAME,20,TimeUnit.SECONDS);

2.如果在java中使用redis 存储时,出现key值乱码,那么是因为没有设置Serializer,以下仅供参考

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="300"></property> <property name="maxWaitMillis" value="3000"></property> <property name="testOnBorrow" value="true"></property> </bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="127.0.0.1"></property> <property name="port" value="6379"></property> <property name="password" value="123456"></property> <property name="usePool" value="true"/> <property name="poolConfig" ref="jedisPoolConfig"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory"></property> <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!! --> <property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!--<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />--> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> <!--开启事务 --> <property name="enableTransactionSupport" value="true"></property> </bean>

 

最后

以上就是包容秀发最近收集整理的关于java redis 常见问题的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部