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);
}
}