Springboot整合Redis缓存机制
文章目录
先放几个必要的依赖吧
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-aop redis.clients jedis 2.9.0 com.baomidou mybatis-plus-boot-starter 3.1.2 mysql mysql-connector-java 8.0.11 com.alibaba druid 1.0.29 com.google.guava guava 27.0.1-jre com.hazelcast hazelcast-all 3.10.1配置文件
**spring: ** application: name: redis-caching **datasource: ** driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/redis_caching?useSSL=FALSE&serverTimezone=GMT%2B8 username: root password: **** type: com.alibaba.druid.pool.DruidDataSource filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 **redis: ** host: 127.0.0.1 port: 6379 password: **** timeout: 10000 **lettuce: ** pool: min-idle: 0 max-idle: 8 max-active: 8 max-wait: -1 **server: ** port: 8080 #mybatis **mybatis-plus: ** mapper-locations: classpath*:/mybatis-mappers/* _#实体扫描,多个package用逗号或者分号分隔 _ typeAliasesPackage: com.guanjian.rediscaching.model **global-config: ** _#数据库相关配置 _ **db-config: ** _#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; _ id-type: INPUT logic-delete-value: -1 logic-not-delete-value: 0 banner: false _#原生配置 _ **configuration: ** map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true jdbc-type-for-null: 'null'
配置类
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Bean public KeyGenerator wiselyKeyGenerator(){ 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(); } }; }
@Bean
public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(port); factory.setPassword(password); return factory; }
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) { RedisCacheManager cacheManager =RedisCacheManager.create(factory); // Number of seconds before expiration. Defaults to unlimited (0) // cacheManager.setDefaultExpiration(10); //设置key-value超时时间 return cacheManager; }
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template); //设置序列化工具,这样ReportBean不需要实现Serializable接口 template.afterPropertiesSet(); return template; }
private void setSerializer(StringRedisTemplate template) {
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); } }
@Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean @Primary public DataSource dataSource() { return new DruidDataSource(); } }
实体类
@Data @TableName("city") public class City implements Serializable{ @TableId private Integer id; private String name; }
dao
@Mapper public interface CityDao extends BaseMapper { }
service
public interface CityService extends IService { }
@Service public class CityServiceImpl extends ServiceImpl<CityDao,City> implements CityService{ }
controller
@RestController public class CityController { @Autowired private CityService cityService; @GetMapping("/findbyid") @Cacheable(cacheNames = "city_info",key = "#id") public City findCityById(@RequestParam("id") int id) { return cityService.getById(id); }
@PostMapping("/save")
@CachePut(cacheNames \= "city\_info",key \= "#city.id")
public City saveCity(@RequestBody City city) {
cityService.save(city);
return city; }
@GetMapping("/deletebyid")
@CacheEvict(cacheNames \= "city\_info",key \= "#id")
public boolean deleteCityById(@RequestParam("id") int id) {
return cityService.removeById(id);
}
@PostMapping("/update")
@CachePut(cacheNames \= "city\_info",key \= "#city.id")
public City updateCity(@RequestBody City city) {
City cityQuery = new City();
cityQuery.setId(city.getId()); QueryWrapper wrapper = new QueryWrapper<>(cityQuery); cityService.update(city,wrapper); return city; } }
测试
我们在数据库中有一个city的表,其中有一条数据

而redis中任何数据都没有

此时我们查询第一个Rest接口

后端日志为
2020-09-30 06:06:12.919 DEBUG 1321 --- [nio-8080-exec-4] c.g.rediscaching.dao.CityDao.selectById : ==> Preparing: SELECT id,name FROM city WHERE id=?
2020-09-30 06:06:12.920 DEBUG 1321 --- [nio-8080-exec-4] c.g.rediscaching.dao.CityDao.selectById : ==> Parameters: 1(Integer)
2020-09-30 06:06:12.945 DEBUG 1321 --- [nio-8080-exec-4] c.g.rediscaching.dao.CityDao.selectById : <== Total: 1
此时我们查询redis中如下

可见我们在没有写任何redis代码的同时,就将数据存储进了redis
此时我们再此查询

则后端日志没有打印SQL语句,说明再次查询是从redis中获取而不是mysql中获取的。
此时我们测试第二个Rest接口

此时数据库中多出一条数据

我们再来看redis中的数据

查询第二条数据可得

现在我们来删除第二条数据

数据库中第二条数据被删除

同时我们在redis中可以看到第二条数据也被删除了

现在我们来修改第一条数据

数据库中同时更新了数据

redis中的数据依然存在

此时我们重新查询第一条数据

后端日志中也没有相应的查询SQL语句,之前的日志如下
2020-09-30 06:32:57.729 DEBUG 1349 --- [nio-8080-exec-3] c.g.rediscaching.dao.CityDao.insert : ==> Preparing: INSERT INTO city ( id, name ) VALUES ( ?, ? )
2020-09-30 06:32:57.730 DEBUG 1349 --- [nio-8080-exec-3] c.g.rediscaching.dao.CityDao.insert : ==> Parameters: 2(Integer), 武汉(String)
2020-09-30 06:32:57.735 DEBUG 1349 --- [nio-8080-exec-3] c.g.rediscaching.dao.CityDao.insert : <== Updates: 1
2020-09-30 06:38:04.042 DEBUG 1349 --- [io-8080-exec-10] c.g.rediscaching.dao.CityDao.deleteById : ==> Preparing: DELETE FROM city WHERE id=?
2020-09-30 06:38:04.043 DEBUG 1349 --- [io-8080-exec-10] c.g.rediscaching.dao.CityDao.deleteById : ==> Parameters: 2(Integer)
2020-09-30 06:38:04.047 DEBUG 1349 --- [io-8080-exec-10] c.g.rediscaching.dao.CityDao.deleteById : <== Updates: 1
2020-09-30 06:40:09.723 DEBUG 1349 --- [nio-8080-exec-3] c.g.rediscaching.dao.CityDao.update : ==> Preparing: UPDATE city SET name=? WHERE id=?
2020-09-30 06:40:09.728 DEBUG 1349 --- [nio-8080-exec-3] c.g.rediscaching.dao.CityDao.update : ==> Parameters: 北京(String), 1(Integer)
2020-09-30 06:40:09.733 DEBUG 1349 --- [nio-8080-exec-3] c.g.rediscaching.dao.CityDao.update : <== Updates: 1
现在我们来给缓存设置过期时间
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Bean public KeyGenerator wiselyKeyGenerator(){ 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(); } }; }
@Bean
public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(port); factory.setPassword(password); return factory; }
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) { Random random = new Random(); return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(factory), //未设置过期策略的在20分钟内过期 getRedisCacheConfigurationWithTtl(1140 + random.nextInt(60)), // 指定 key 策略 getRedisCacheConfigurationMap() ); }
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template); //设置序列化工具,这样ReportBean不需要实现Serializable接口 template.afterPropertiesSet(); return template; }
private void setSerializer(StringRedisTemplate template) {
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); }
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new ConcurrentHashMap<>();
Random random = new Random(); redisCacheConfigurationMap.put("city_info", getRedisCacheConfigurationWithTtl(540 + random.nextInt(60))); return redisCacheConfigurationMap; }
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
Jackson2JsonRedisSerializer<Object> 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); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith( RedisSerializationContext .SerializationPair .fromSerializer(jackson2JsonRedisSerializer) ).entryTtl(Duration.ofSeconds(seconds)); return redisCacheConfiguration; } }
通过查看redis的键的过期时间,我们可以看到

它是用的指定键的过期时间
此时我们调整RedisConfig的内容,将指定的city_info改掉
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new ConcurrentHashMap<>(); Random random = new Random(); redisCacheConfigurationMap.put("abcd", getRedisCacheConfigurationWithTtl(540 + random.nextInt(60))); return redisCacheConfigurationMap; }
此时我们会使用默认的20分钟过期时间

此时我们可以看到,它使用的就是默认所有键都相同的20分钟过期时间。
现在我们来增加防止缓存高并发的功能
缓存高并发的一般性原则可以参考建立缓存,防高并发代码demo
现在我们要通过标签来完成这个功能,新增一个标签
@Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface Lock { }
新增一个Redis工具类,包含了分布式锁的实现
@Component public class RedisUtils { @Autowired private RedisTemplate redisTemplate; private static final Long RELEASE_SUCCESS = 1L; private static final String UNLOCK_LUA = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; _/** _ * 写入缓存 */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } _/** _ * 写入缓存设置时效时间 */ public boolean set(final String key, Object value, Long expireTime , TimeUnit timeUnit) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, timeUnit); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
_/\*\*
_ * 写入缓存设置时效时间,仅第一次有效 * @param _key _ * @param _value _ * @param _expireTime _ * @param _timeUnit _ * _@return _ _*/ _ public boolean setIfAbsent(final String key, Object value, Long expireTime , TimeUnit timeUnit) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.setIfAbsent(key,value,expireTime,timeUnit); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } _/** _ * 批量删除对应的value */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } _/** _ * 批量删除key */ public void removePattern(final String pattern) { Set keys = redisTemplate.keys(pattern); if (keys.size() > 0){ redisTemplate.delete(keys); } } _/** _ * 删除对应的value */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } _/** _ * 判断缓存中是否有对应的value */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } _/** _ * 读取缓存 */ public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } _/** _ * 哈希 添加 */ public void hmSet(String key, Object hashKey, Object value){ HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); hash.put(key,hashKey,value); } _/** _ * 哈希获取数据 */ public Object hmGet(String key, Object hashKey){ HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); return hash.get(key,hashKey); } _/** _ * 列表添加 */ public void lPush(String k,Object v){ ListOperations<String, Object> list = redisTemplate.opsForList(); list.rightPush(k,v); } _/** _ * 列表获取 */ public List
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论