java redis 常见问题

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

   原因:redis与spring版本不匹配

 

        <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.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,以下仅供参考

<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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值