基於Spring Boot 1.5.2.RELEASE版本,一方面驗證與Redis的集成方法,另外瞭解使用方法。
集成方法
配置依賴
修改
pom.xml
,增加如下內容。<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置Redis
修改
application.yml
,增加如下內容。spring: redis: host: localhost port: 6379 pool: max-idle: 8 min-idle: 0 max-active: 8 max-wait: -1
配置Redis緩存
package net.jackieathome.cache; import java.lang.reflect.Method; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; @Configuration @EnableCaching // 啓用緩存特性 public class RedisConfig extends CachingConfigurerSupport { // 緩存數據時Key的生成器,可以依據業務和技術場景自行定製 // @Bean // public KeyGenerator customizedKeyGenerator() { // return new KeyGenerator() { // @Override // public Object generate(Object target, Method method, Object... params) { // StringBuilder sb = new StringBuilder(); // sb.append(target.getClass().getName()); // sb.append(method.getName()); // for (Object obj : params) { // sb.append(obj.toString()); // } // return sb.toString(); // } // }; // // } // 定製緩存管理器的屬性,默認提供的CacheManager對象可能不能滿足需要 // 因此建議依賴業務和技術上的需求,自行做一些擴展和定製 @Bean public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); redisCacheManager.setDefaultExpiration(300); return redisCacheManager; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); 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); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
驗證集成後的效果
考慮到未來參與的項目基於MyBatis實現數據庫訪問,而利用緩存,可有效改善Web頁面的交互體驗,因此設計瞭如下兩個驗證方案。
方案一
在訪問數據庫的數據對象上增加緩存註解,定義緩存策略。從測試效果看,緩存有效。
頁面控制器
package net.jackieathome.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import net.jackieathome.bean.User; import net.jackieathome.dao.UserDao; import net.jackieathome.db.mapper.UserMapper; @RestController public class UserController { @Autowired private UserDao userDao; @RequestMapping(method = RequestMethod.GET, value = "/user/id/{id}") public User findUserById(@PathVariable("id") String id) { return userDao.findUserById(id); } @RequestMapping(method = RequestMethod.GET, value = "/user/create") public User createUser() { long time = System.currentTimeMillis() / 1000; String id = "id" + time; User user = new User(); user.setId(id); userDao.createUser(user); return userDao.findUserById(id); } }
Mapper定義
package net.jackieathome.db.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import net.jackieathome.bean.User; @Mapper public interface UserMapper { void createUser(User user); User findUserById(@Param("id") String id); }
數據訪問對象
package net.jackieathome.dao; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.annotations.Param; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import net.jackieathome.bean.User; import net.jackieathome.db.mapper.UserMapper; @Component @CacheConfig(cacheNames = "users") @Transactional public class UserDao { private static final Logger LOG = LoggerFactory.getLogger(UserDao.class); @Autowired private UserMapper userMapper; @CachePut(key = "#p0.id") public void createUser(User user) { userMapper.createUser(user); LOG.debug("create user=" + user); } @Cacheable(key = "#p0") public User findUserById(@Param("id") String id) { LOG.debug("find user=" + id); return userMapper.findUserById(id); } }
方案二
直接在Mapper定義上增加緩存註解,控制緩存策略。從測試效果看,緩存有效,相比於方案一,測試代碼更加簡潔一些。
頁面控制器
package net.jackieathome.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import net.jackieathome.bean.User; import net.jackieathome.dao.UserDao; import net.jackieathome.db.mapper.UserMapper; @RestController public class UserController { @Autowired private UserMapper userMapper; @RequestMapping(method = RequestMethod.GET, value = "/user/id/{id}") public User findUserById(@PathVariable("id") String id) { return userMapper.findUserById(id); } @RequestMapping(method = RequestMethod.GET, value = "/user/create") public User createUser() { long time = System.currentTimeMillis() / 1000; String id = "id" + time; User user = new User(); user.setId(id); userMapper.createUser(user); return userMapper.findUserById(id); } }
Mapper定義
package net.jackieathome.db.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import net.jackieathome.bean.User; @CacheConfig(cacheNames = "users") @Mapper public interface UserMapper { @CachePut(key = "#p0.id") void createUser(User user); @Cacheable(key = "#p0") User findUserById(@Param("id") String id); }
總結
上述兩個測試方案並沒有優劣之分,僅是爲了驗證緩存的使用方法,體現了不同的控制粒度,在實際的項目開發過程中,需要依據實際情況做不同的決斷。
緩存相關的註解:
- CacheConfig
- Cacheable
- CachePut
- CacheEvict
參考資料
使用redis緩存
- Caching
- spring-boot/spring-boot-samples/spring-boot-sample-cache
- Caching Data in Spring Using Redis
- Spring Boot使用redis做數據緩存
- Spring-boot用Redis 作爲緩存服務
- Spring Boot中的緩存支持(二)使用Redis做集中式緩存
- Spring Boot使用redis做數據緩存
- Spring Boot下配置Redis緩存
MyBatis的緩存特性
- MyBatis Ehcache Adapter - Reference Documentation
- Mapper XML 文件
- mybatis緩存的使用及理解
- mybatis緩存
- MyBatis 緩存機制深度解剖 / 自定義二級緩存
- MyBatis的緩存配置(Cache)
- Mybatis二級緩存原理
- 深入淺出Mybatis-改造Cache
- mybatis的緩存機制(一級緩存二級緩存和刷新緩存)和mybatis整合ehcache
- 《深入理解mybatis原理》 MyBatis緩存機制的設計與實現
- 《深入理解mybatis原理》 MyBatis的二級緩存的設計原理
- mybatis入門基礎(八)—–查詢緩存
其它
運行redis docker鏡像的命令
sudo docker run --rm -d -p 6379:6379 redis