前言:在开发后台的时候,通过Redis对分页查询的数据进行了缓存,但是数据在做新增、删除、更新的时候,为了保持Redis中缓存的数据与数据库中的数据保持一致性,需要对缓存也进行更新处理。
这里我用的更新方法也是非常简单暴力,就在查询的时候,会对Redis进行一次查询,根据分页的页码和相对应的表。如果查到的缓存为空,那么就会去数据库查询,然后再设置缓存。
而在增删改的接口上,我是直接将缓存删除,等到调用分页查询数据的接口的时候再去重新设置一次缓存,以此保持数据的一致性。
具体可以看看下面的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| @GetMapping("/page") public Result page(@RequestParam("pageNum") Integer pageNum , @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "username", required = false) String username, @RequestParam(value = "email", required = false) String email, @RequestParam(value = "phone", required = false) String phone){ IPage<User> page = new Page<>(pageNum , pageSize); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.like(!Strings.isEmpty(username),"username" , username); queryWrapper.like(!Strings.isEmpty(email),"email" , email); queryWrapper.like(!Strings.isEmpty(phone),"phone", phone);
if(username == "" && email == "" && phone == ""){ String userKey = Constants.USER_PAGE_KEY + "_" + String.valueOf(pageNum); String res = iRedisService.getString(userKey); if(!StrUtil.isBlank(res)){ JSONObject data = JSONUtil.parseObj(res); Integer nowSize = (Integer) data.get("size"); if(nowSize != pageSize){ iRedisService.flushRedis(userKey); IPage<User> userIPage = userService.page(page, queryWrapper); iRedisService.setString(userKey , JSONUtil.toJsonStr(userIPage)); return Result.success(userIPage); } return Result.success(data); } else { IPage<User> userIPage = userService.page(page, queryWrapper); iRedisService.setString(userKey , JSONUtil.toJsonStr(userIPage)); return Result.success(userIPage); } }
return Result.success(userService.page(page, queryWrapper)); }
|
实现方法
- 加入Redis序列化配置类,再Config里面设置相对于的序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration public class RedisConfig {
@Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.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);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate; } }
|
- Redis的工具类里面加入下面的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public Set<String> getKeysByPre(String prefix){ return stringRedisTemplate.keys(prefix + "*"); }
public void deleteByPre(String prefix){ Set<String> keys = stringRedisTemplate.keys(prefix + "*"); stringRedisTemplate.delete(keys); }
|
- 然后传入要模糊搜索的key关键字,查询到的keys会放在一个列表里面,调用delete方法会删除对应的数据