(原)Redis - 【三】Redis + MySQL + SpringBoot 项目中 基本的CRUD使用
目录
一、CRUD目的
二、代码
一、CRUD目的
通过redis的缓存数据库的使用,减少DB的访问量,进而将实现数据的同步缓存,所以使用CRUD来操作redis的数据
二、代码
@Overridepublic WxPlatform SelectWxDefaultPlatformInfo(String wxAgentAddrId) {WxAgentAddress wxAgentAddress = wxAgentAddressService.getDefaultPlatform(wxAgentAddrId); WxPlatform platform = wxPlatformRepository.getWxDafaultPlatformInfo(wxAgentAddress.getWx_areaId()); // 判断key是否存在 if(!redisServiceImpl.exists(RedisServiceKey.WxPlatform_REDIS_KEY)) { // 获取key的valueWxPlatform wxPlat = (WxPlatform) redisServiceImpl.get(RedisServiceKey.WxPlatform_REDIS_KEY);if(wxPlat!=null && !wxPlat.getWx_agentAddressId().equals(wxAgentAddrId)) {// 删除keyredisServiceImpl.remove(RedisServiceKey.WxPlatform_REDIS_KEY);}} // 保存keyredisServiceImpl.set(RedisServiceKey.WxPlatform_REDIS_KEY, platform); //WxPlatform wxPlat = (WxPlatform) redisServiceImpl.get(RedisServiceKey.WxPlatform_REDIS_KEY);return platform;}
三、潜在规则
在操作redis中的key时,每个key都要去判断下hashkey是否存在,然后进行业务的相关操作
Java Source Code: org.springframework.data.redis.hash.JacksonHashMapper
Redis的StringRedisTemplate在SpringBoot中的使用完全整理之opsForHash_Lane_Wang的博客-CSDN博客