springboot整合redis和springCache
阿新 • • 發佈:2018-12-10
1.在配置檔案中添加註解@EnableCaching (RedisConfig.class)
/** * Created by zhuzhengping on 2017/2/19. * redis配置 */ @Configuration @EnableCaching //這個使用springCache----->如果只用redis,可以不用新增 @PropertySource("classpath:config/redis.properties") public class RedisConfig2 extends CachingConfigurerSupport { @Bean @Override public KeyGenerator keyGenerator(){ return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for (Object obj:objects){ sb.append(obj.toString()); } System.out.println("keyGenerator=" + sb.toString()); return sb.toString(); } }; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate){ RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); //配置CacheManneg來配置預設的過期時間和針對每個快取容器(value) redisCacheManager.setDefaultExpiration(60); Map<String,Long> expiresMap=new HashMap<>(); expiresMap.put("Product",5L); redisCacheManager.setExpires(expiresMap); return redisCacheManager; } @Autowired private Environment env; @Bean public RedisConnectionFactory redisConnectionFactory(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName(env.getProperty("redis.host").trim()); jedisConnectionFactory.setPort(Integer.parseInt(env.getProperty("redis.port").trim())); jedisConnectionFactory.setPassword(env.getProperty("redis.password").trim()); jedisConnectionFactory.setDatabase(Integer.parseInt(env.getProperty("redis.database").trim())); jedisConnectionFactory.setUsePool(true); jedisConnectionFactory.setPoolConfig(jedisPoolConfig); return jedisConnectionFactory; } /* @Bean public RedisTemplate<String,String> redisTemplate(){ //StringRedisTemplate的構造方法中預設設定了stringSerializer RedisTemplate<String,String> redisTemplate = new StringRedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory()); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; }*/ /** * 配置RedisTemplate * 設定新增序列化器 * key 使用string序列化器 * value 使用Json序列化器 * 還有一種簡答的設定方式,改變defaultSerializer物件的實現。 * @return */ @Bean public RedisTemplate<String, Object> redisTemplate() { //StringRedisTemplate的構造方法中預設設定了stringSerializer RedisTemplate<String, Object> template = new RedisTemplate<>(); //set key serializer StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); //set value serializer template.setDefaultSerializer(jackson2JsonRedisSerializer); template.setConnectionFactory(redisConnectionFactory()); template.afterPropertiesSet(); return template; } }
2.通過註解的方式快取
package com.xpf.services; import com.xpf.config.RedisBaseDao; import com.xpf.config.RedisKeyPrefix; import com.xpf.dao.UserInfoAutoDao; import com.xpf.domain.UserInfo; import com.xpf.mapper.UserInfoMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; @Service @CacheConfig(cacheNames = "user") public class UserInfoServices { @Autowired UserInfoAutoDao userInfoAutoDao; @Autowired UserInfoMapper userInfoMapper; @Resource(name = "redisTemplate") protected ValueOperations<String, UserInfo> valueOperations; @Autowired RedisBaseDao redisBaseDao; @Autowired RedisTemplate redisTemplate; public UserInfo getUser(Long id) { String key = RedisKeyPrefix.User + id; /** *判斷redis中是否存在該 快取 */ Boolean hasKey = redisTemplate.hasKey(key); if (hasKey) { System.out.println("----------redis中取的key----------"); UserInfo userInfo= valueOperations.get(key);//(UserInfo) redisTemplate.opsForValue().get(key); return userInfo; } UserInfo userInfo=userInfoAutoDao.findUser(id); redisTemplate.opsForValue().set(key,userInfo,600, TimeUnit.SECONDS); return userInfo; } /** * 通過整合springCache,實現註解式快取 * 通過id查詢,如果查詢到則進行快取 * @param id 實體類id * @return 查詢到的實現類 */ @Cacheable(key = "\"user_\" + #id") public UserInfo getUserCache(Long id) { UserInfo userInfo=userInfoAutoDao.findUser(id); return userInfo; } /* * 通過id查詢,如果查詢到則進行快取,並且設定快取的有效期 * 值屬性上用#號隔開,第一個是原始的快取容器名稱,第二個是快取的有效時間,第三個是快取的自動重新整理時間,單位都是秒。 * @param id 實體類id */ @Cacheable(value = "xpf#10#15",key = "\"user_\" + #id") public UserInfo getUserCacheTimeout(Long id) { System.out.println("-------------從這裡訪問資料庫-----------"); UserInfo userInfo=userInfoAutoDao.findUser(id); return userInfo; } /** * 單個user物件的插入操作,使用user+id * @param user * @return */ @CachePut(key = "\"user_\" + #user.uid") public UserInfo saveUser(UserInfo user){ userInfoMapper.insert(user); return user; } @CacheEvict(key = "\"user_\" + #id") public void deleteById(Long id){ userInfoMapper.deleteByPrimaryKey(id); } }
3.測試
@Test public void testCache() { System.out.println("=======第一次呼叫======"); System.out.println(services.getUserCache(Long.valueOf(3)).toString()); System.out.println("===========第二次呼叫======="); System.out.println(services.getUserCache(Long.valueOf(3)).toString()); } @Test public void testCacheTimeout() { System.out.println("=======第一次呼叫======"); System.out.println(services.getUserCacheTimeout(Long.valueOf(3)).toString()); System.out.println("===========第二次呼叫======="); System.out.println(services.getUserCacheTimeout(Long.valueOf(3)).toString()); }