配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:c="http://www.springframework.org/schema/c" 6 xmlns:cache="http://www.springframework.org/schema/cache" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/cache 10 http://www.springframework.org/schema/cache/spring-cache.xsd"> 11 12 <!-- 以前项目中的配置,注意需要添加Spring Data Redis等jar包 --> 13 <description>redis配置</description> 14 15 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 16 <property name="maxIdle" value="${redis.pool.maxIdle}"/> 17 <property name="maxTotal" value="${redis.pool.maxActive}"/> 18 <property name="maxWaitMillis" value="${redis.pool.maxWait}"/> 19 <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> 20 <property name="testOnReturn" value="${redis.pool.testOnReturn}"/> 21 </bean> 22 23 <!-- JedisConnectionFactory --> 24 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 25 <property name="hostName" value="${redis.master.ip}"/> 26 <property name="port" value="${redis.master.port}"/> 27 <property name="poolConfig" ref="jedisPoolConfig"/> 28 </bean> 29 30 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" 31 p:connectionFactory-ref="jedisConnectionFactory"> 32 <property name="keySerializer"> 33 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean> 34 </property> 35 <property name="valueSerializer"> 36 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 37 </property> 38 <property name="hashKeySerializer"> 39 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 40 </property> 41 <property name="hashValueSerializer"> 42 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 43 </property> 44 </bean> 45 46 <!--spring cache--> 47 <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" 48 c:redisOperations-ref="redisTemplate"> 49 <!-- 默认缓存10分钟 --> 50 <property name="defaultExpiration" value="600"/> 51 <property name="usePrefix" value="true"/> 52 <!-- cacheName 缓存超时配置,半小时,一小时,一天 --> 53 <property name="expires"> 54 <map key-type="java.lang.String" value-type="java.lang.Long"> 55 <entry key="halfHour" value="1800"/> 56 <entry key="hour" value="3600"/> 57 <entry key="oneDay" value="86400"/> 58 <!-- shiro cache keys --> 59 <entry key="authorizationCache" value="1800"/> 60 <entry key="authenticationCache" value="1800"/> 61 <entry key="activeSessionCache" value="1800"/> 62 </map> 63 </property> 64 </bean> 65 66 <!-- cache注解,和spring-ehcache.xml中的只能使用一个 --> 67 <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/> 68 </beans>
使用注解
1 package com.wangzhixuan.service.impl; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.cache.annotation.Cacheable; 8 import org.springframework.stereotype.Service; 9 10 import com.wangzhixuan.mapper.UserMapper; 11 import com.wangzhixuan.model.User; 12 13 @Service 14 public class TestService { 15 @Autowired 16 private UserMapper userMapper; 17 18 @Cacheable(value = "hour", key = "#id") 19 public User selectById(Serializable id) { 20 return userMapper.selectById(id); 21 } 22 @Cacheable(value = "hours") 23 public String date() { 24 return new Date().toString(); 25 } 26 }