Spring Boot的缓存之旅(二)
使用Redis
1.引入spring-boot-starter-data-redis依赖
2.进行项目文件配置
使用前先启动redis
#redis配置 #redis服务器地址 spring.redis.host=localhost #redis服务器端口 spring.redis.port=6379 #数据库配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456abc ##validate 加载hibernate时,验证创建数据库表结构 ##create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 ##create-drop 加载hibernate时创建,退出是删除表结构 ##update 加载hibernate自动更新数据库结构 ##none 启动时不做任何操作 spring.jpa.hibernate.ddl-auto=create ##控制台打印sql spring.jpa.show-sql=true
3.使用redis的set和get方法
@Service public class RedisService { @Resource private RedisTemplate<String, Object> redisTemplate; //或者 这时不能有<String, Object>,这个特殊 // @Autowired // private RedisTemplate redisTemplate; public void set(String key,Object value) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class)); ValueOperations<String, Object> vo=redisTemplate.opsForValue(); vo.set(key, value); } public void set(String key,Object value,Long time,TimeUnit t) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class)); ValueOperations<String, Object> vo=redisTemplate.opsForValue(); vo.set(key, value); } public Object get(String key) { ValueOperations<String, Object> vo=redisTemplate.opsForValue(); return vo.get(key); } }
4.编写一个User实体类和repository
5.Controller测试
@RestController public class UserController { @Autowired private RedisService redisService; @Autowired private UserRepository userRepository; @GetMapping(value = "saveUser") public String saveUser(Long id, String userName, String userPassword) { User user = new User(id, userName, userPassword); redisService.set(id.toString(), user); return "success"; } @GetMapping(value = "getUserById") public Object getUserById(Long id) { return redisService.get(id.toString()); } @GetMapping("/saveUser2") public User saveUser2(Long id, String userName, String userPassword) { User user = new User(id, userName, userPassword); userRepository.save(user); return user; } @GetMapping(value = "getUser") public Object getUser(Long id) { Object object = redisService.get(id.toString()); if (object == null) { object = (userRepository.findById(id)).get(); if (object != null) { redisService.set(id.toString(), object, 100L, TimeUnit.SECONDS); } } return object; } }
注意对键和值都要进行序列化。
使用Redis缓存
刚只是把redis当数据库用,而真实环境中一般以传统数据库为主,以redis数据库为辅。redis通常起缓存作用。
1.在RedisService类新增一个设置过期时间的方法:
public void set(String key,Object value,Long time,TimeUnit t) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class)); ValueOperations<String, Object> vo=redisTemplate.opsForValue(); vo.set(key, value); }
2.在Controller类里新增两个方法
@GetMapping("/saveUser2") public User saveUser2(Long id, String userName, String userPassword) { User user = new User(id, userName, userPassword); userRepository.save(user); return user; } @GetMapping(value = "getUser") public Object getUser(Long id) { Object object = redisService.get(id.toString()); if (object == null) { object = (userRepository.findById(id)).get(); if (object != null) { redisService.set(id.toString(), object, 100L, TimeUnit.SECONDS); } } return object; }