<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.3.RELEASE</version> </dependency>
springboot 1.5之后的版本引入的是java
spring-boot-starter-data-redis
配置都是大同小异,更改为本身合理的便可;redis
1 ##Redis 配置 2 spring.redis.database=0 3 spring.redis.host=127.0.0.1 4 spring.redis.port=6379 5 spring.redis.password=asdf1234 6 #链接池最大链接数 7 spring.redis.jedis.pool.max-active=8 8 #链接池最大阻塞等待时间,(负值没有限制) 9 spring.redis.jedis.pool.max-wait=-1 10 #链接池最大空闲链接 11 spring.redis.jedis.pool.max-idle=8 12 #链接池最小空闲链接 13 spring.redis.jedis.pool.min-idle=0 14 #链接超时时间 15 spring.redis.timeout=5000
这里只是简单的封装了经常使用的几个方法,以后也好根据须要进行扩展; spring
1 @Service 2 public class catchService { 3 @Autowired 4 RedisTemplate<Object, Object> redisTemplate; 5 6 public void set(String key, Object value){ 7 redisTemplate.opsForValue().set(key, value); 8 } 9 public void set(String key, Object value, long timeout) { 10 set(key,value,timeout, TimeUnit.MINUTES); 11 } 12 public void set(String key, Object value, long timeout, TimeUnit timeUnit) { 13 redisTemplate.opsForValue().set(key, value, timeout, timeUnit); 14 } 15 16 public Object get(String key) { 17 return redisTemplate.opsForValue().get(key); 18 } 19 20 public boolean hasKey(String key) { 21 return redisTemplate.hasKey(key); 22 } 23 24 public boolean deleteKey(String key) { 25 return redisTemplate.delete(key); 26 } 27 28 }
1 package com.wzc.springboot_redis.service; 2 3 import com.wzc.springboot_redis.entity.City; 4 import com.wzc.springboot_redis.mapper.cityMapper; 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Service; 9 10 import java.util.concurrent.TimeUnit; 11 12 13 @Service 14 public class cityService { 15 private static final Logger LOGGER = LoggerFactory.getLogger(cityService.class); 16 @Autowired 17 private cityMapper cityDao; 18 19 @Autowired 20 private catchService catchService; 21 22 23 public City getCity(String id) { 24 String key = "city_" + id; 25 boolean hasKey = catchService.hasKey(key); 26 if(hasKey) { 27 City city = (City) catchService.get(key); 28 LOGGER.info("cityService.getCity(): 获取城市信息 from catch >> " + city); 29 return city; 30 }else { 31 City city = cityDao.getCity(id); 32 catchService.set(key, city,15, TimeUnit.SECONDS); 33 LOGGER.info("cityService.getCity(): 信息插入缓存 >> " + city); 34 return city; 35 } 36 } 37 38 public City updateCity(Long id, String cityName, Long provinceId, String description) { 39 City city = new City(); 40 city.setId(id); 41 city.setCityName(cityName); 42 city.setProvinceId(provinceId); 43 city.setDescription(description); 44 cityDao.updateCity(city); 45 46 String key = "city_" + id; 47 if(catchService.hasKey(key)) { 48 catchService.deleteKey(key); 49 } 50 return city; 51 52 } 53 }
配置类的东西说完,如今要 考虑何时须要考虑使用 缓存:sql
通常来看缓存是用来缓解数据库的访问压力的,当系统或网站的处理和访问量很是大的时候,其中查询操做是一个系统最经常使用到的处理,就会频繁的读取数据库,这将给数据库带来巨大的压力,经过缓存频繁访问的数据存储到缓存中从而能缓解数据库的压力。数据库
还有就是用Redis作一个临时存储的工具用,例如我在处理系统Token时,使用的时黑名单方法,将用户注销掉的Token暂时存储到Redis中,以备以后访问判断。缓存
固然还有好多能用到缓存的地方。springboot
Redis其实就是说把表中常常访问的记录放在了Redis中,而后用户查询时先去查询Redis再去查询MySQL,确实实现了读写分离,也就是Redis只作读操做。因为缓存在内存中,因此查询会很快。app
如何肯定在redis查询仍是Mysql查询:对于一个sql语句格式的数据请求,首先计算该语句的须要查询对象的标识符,而后利用该标识符在Redis中查找该结果集。注意,结果集中的每一行都有一个相应的键,这些键都存储在一个Redis集合结构中。若是Redis中不存在这样一个集合,说明要找的结果集不在Redis中,因此须要执行相应的sql语句,在Mysql中查询到相应的结果集,而后按照上面所说的办法把结果集中的每一行以字符串或哈希的形式存入Redis。spring-boot