Spring Cache注解+Redis(二)

之前有写过一篇Spring Cache注解+Redis

今天对Cache+Redis配置的优化。

 

首页还是Jar的依赖,请看之前的文章,这里不做赘述。

然后后XML的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"
       default-autowire="byName">

    <context:property-placeholder
            location="classpath*:config/redis.properties" ignore-unresolvable="true"/>

    <!-- 启用缓存注解功能 -->
    <cache:annotation-driven cache-manager="cacheManager"/>

    <!--Redis Cache 配置-->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate"/>
    </bean>

    <!-- redis 相关配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="poolConfig"/>
        <property name="port" value="${redis.port}"/>
        <property name="hostName" value="${redis.host}"/>
        <property name="timeout" value="${redis.timeout}"/>
        <property name="password" value="${redis.pwd}"/>
        <property name="database" value="${redis.database}"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <!--<property name="defaultSerializer" ref="stringRedisSerializer"/>-->
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <!--<property name="valueSerializer" ref="stringRedisSerializer"/>-->
        <property name="hashKeySerializer" ref="stringRedisSerializer"/>
        <!--<property name="hashValueSerializer" ref="stringRedisSerializer"/>-->
    </bean>

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

</beans>

 redis.properties请看上一篇文章;

 

 

缓存注解使用:

在service实现方法上加注解

 

    @Override
    @Cacheable(value = "ShardingTableCache", key = "'shardingTableName:' + #appId + ':' + #appUserId")
    public Integer getShardingTableName(String appId, String appUserId) {
        return mapper.getShardingTableName(appId, appUserId);
    }

    @Override
    @CacheEvict(value = "ShardingTableCache", key = "'shardingTableName:' + #appId + ':' + #appUserId")
    public int updateShardingTable(String appId, String appUserId, int shardingTable) {
        return mapper.updateShardingTable(appId, appUserId, shardingTable);
    }

 

 

使用redis客户端RedisDesktopManager查看效果如下图:



 

中间遇到的问题: 插入的hash类型的key前面会有一堆的\xac\xed\x00\x05t\x00\tb 这种东西

处理方法:redisTemplate中添加<property name="keySerializer" ref="stringRedisSerializer"/>等

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值