Redis in spring

maven项目中在pom.xml中依赖2个jar包,其他的spring的jar包省略:

 <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-redis</artifactId>
 </dependency>

spring-redis.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:c='http://www.springframework.org/schema/c'
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:tx="http://www.springframework.org/schema/tx"
    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 
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">

    <cache:annotation-driven />      
    <bean id="baseNameGenerator" class="com.common.PackageBeanNameGenerator" />
    <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
    p:use-pool="true" p:host-name='${redis.host}' p:port='${redis.port}' p:password="${redis.pass}"/>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" 
    p:connection-factory-ref="jedisConnFactory"/> 
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg index="0" ref="redisTemplate"></constructor-arg>
        <property name="defaultExpiration" value="86400"/>
        <property name="expires">
            <map>
                <entry key="t_user_tag" value="604800" />       
            </map>
        </property>
        <property name="usePrefix" value="true" />
    </bean>
</beans>

redis-config.properties中的内容:

redis.host=127.0.0.1
redis.port=6379
redis.pass=

CacheKey :

public class CacheKeys {
    public static final String T_USER_TAGS = "t_user_tags";
}

在service的实现类中:

@Service
public class TagService extends BaseServiceSupport<Tag, Integer>{
    @Autowired
    TagMapper tagMapper;

    @Override
    public BaseMapper<Tag, Integer> getMapper() {
        return tagMapper;
    }

    @Cacheable(value = CacheKeys.T_USER_TAGS)
    public List<Tag> getByUpdaterId(Integer updaterId){
        return tagMapper.getByUpdaterId(updaterId);
    }

    @CacheEvict(value = CacheKeys.T_USER_TAGS, key="#record.updaterId")
    public Integer insertSelective(Tag record){
        return tagMapper.insertSelective(record);
    }

    @CacheEvict(value = CacheKeys.T_USER_TAGS,  key="#record.updaterId")
    public Integer updateByPrimaryKeySelective(Tag record){
        return tagMapper.updateByPrimaryKeySelective(record);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值