作为苦逼的程序猿,有时候我们需要给自己减压,对于数据库查询或者调用第三方查询 一些不经常变动的信息,但是这些查询又是很频繁,这时候我们可能会用到缓存。spring+Redis给我们提供了一个很简单的注解缓存。接下来让我们看看多门的简单
直接上干货:
Redis配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:redis="http://www.springframework.org/schema/redis" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd" > <cache:annotation-driven/> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.hostName}"/> <property name="port" value="${redis.port}"/> <property name="password" value="${redis.password}"/> <property name="database" value="${redis.database}"/> <property name="poolConfig"> <bean class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/> <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> </bean> </property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisConnectionFactory"/> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <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> </bean> </beans>
Java配置代码
1、前缀配置(此项可以更好的从可视化Redis工具如:rdm中查看缓存信息)
public class MyRedisPrefix implements RedisCachePrefix { private final RedisSerializer serializer; private final String delimiter; public MyRedisPrefix() { this(":"); } public MyRedisPrefix(String delimiter) { this.serializer = new StringRedisSerializer(); this.delimiter = delimiter; } @Override public byte[] prefix(String cacheName) { return this.serializer.serialize(this.delimiter != null ? this.delimiter.concat(":").concat(cacheName).concat(":") : cacheName.concat(":")); } }
2、缓存key值及其时间配置
@Configuration public class RedisCacheConfig { private static long ONE_DAY = 60L * 60L * 24L; private static final String projectName = "test"; @Autowired private RedisTemplate redisTemplate; @Bean public CacheManager cacheManager() { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); Map<String, Long> expires = new HashMap<>(); expires.put("cache_something", ONE_DAY); // 设置超时 cacheManager.setExpires(expires); // 没有设置的缓存默认过期时间 cacheManager.setDefaultExpiration(60); cacheManager.setCachePrefix(new RedisPrefix(projectName)); cacheManager.setUsePrefix(true); return cacheManager; } }
3、缓存服务类
@Service public class CacheService { @Cacheable(value = "cache_something", key = "'something'", unless = "#result == null") public String getHuPoToken() { String something = getSomething(); return something; } private String getSomething() { String something = "this is something " + DateUtils.formatTime(new Date()); System.out.println("【测试缓存】未进入缓存,实时查询结果:{}" + something); return something; } }
4、调用缓存服务
public class TestCache extends SpringBaseHttpTest { @Autowired private CacheService cacheService; @org.junit.Test public void testcache() throws Exception { cacheService.getCacheSomething(); } }