Redis实战-详细配置-优雅的使用Redis注解/RedisTemplate (4)

CacheConstant.USER_CACHE_KEY_PREFIX + "' + #userId":其key = 指定前缀 + 当前方法实参(userId)。

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ldx.redis.constant.CacheConstant; import com.ldx.redis.entity.SysUser; import com.ldx.redis.mapper.SysUserMapper; import com.ldx.redis.service.SysUserService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Service; import java.util.List; /** * 用户管理实现 * @author ludangxin * @date 2021/8/11 */ @Slf4j @Service @RequiredArgsConstructor @CacheConfig(cacheNames = CacheConstant.USER_CACHE_NAME) public class SysUserServiceImpl implements SysUserService { private final SysUserMapper userMapper; @Override @Cacheable(key = "'" + CacheConstant.USER_CACHE_KEY_PREFIX + "all'", unless = "#result.size() == 0") public List<SysUser> queryAll() { log.debug("查询全部用户信息~"); LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>(); return userMapper.selectList(queryWrapper); } @Override @Cacheable(key = "'" + CacheConstant.USER_CACHE_KEY_PREFIX + "' + #userId", unless = "#result == null") public SysUser getUserInfo(Long userId) { log.debug("查询用户:{} 详情", userId); return userMapper.selectById(userId); } @Override @CacheEvict(key = "'" + CacheConstant.USER_CACHE_KEY_PREFIX + "all'") public void add(SysUser user) { log.debug("新增用户:{}", user.getNickName()); userMapper.insert(user); } @Override @Caching(evict = {@CacheEvict(key = "'" + CacheConstant.USER_CACHE_KEY_PREFIX + "all'"), @CacheEvict(key = "'" + CacheConstant.USER_CACHE_KEY_PREFIX + "' + #userId") }) public void update(Long userId, SysUser user) { log.debug("更新用户:{}", user.getNickName()); user.setId(userId); userMapper.updateById(user); } @Override @Caching(evict = {@CacheEvict(key = "'" + CacheConstant.USER_CACHE_KEY_PREFIX + "all'"), @CacheEvict(key = "'" + CacheConstant.USER_CACHE_KEY_PREFIX + "' + #userId") }) public void delete(Long userId) { log.debug("删除用户:{}", userId); userMapper.deleteById(userId); } } 2.6.2 SysRoleServiceImpl

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyxfyy.html