Redis的连接与使用,呈现数据字典信息。@Cacheable先查缓存,缓存中若没有则查数据库

连接redis

docker ps 查看redis的pid

  1. docker exec -it c74a901d8026 bash
  2. redis-cli
  3. get *
  4. set a b
  5. get a
    在这里插入图片描述

@Cacheable 直接查缓存,无数据再查数据库
@CachePut 增
@CacheEvict 删改

在ServiceImpl中使用redis缓存

/**
 * @Author Weton Li
 * @Date 2021/4/13 17:38
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    /**
     * 根据id查询孩子数据
     *
     * @param id
     * @return
     */
    @Override
    @Cacheable(value = "dict",keyGenerator = "keyGenerator") // 先查缓存
    public List<Dict> findChildData(Long id) {

        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id); // 将数据库中的id,和参数id对比
        List<Dict> list = baseMapper.selectList(wrapper);


        for (Dict dict : list) {
            Long dictId = dict.getId(); // 此id非彼参数id
            boolean isHave = isChild(dictId); // 调用isChild()方法
            dict.setHasChildren(isHave);
        }
        return list;
    }

sudo docker run -p 6379:6379 -v /etc/config/redis.conf -d 1319b1eaa0b7 /etc/config/redis.conf

在linux系统/etc/config/下创建redis.conf,使用vi编辑器编辑,如下:
在这里插入图片描述
如果不手动配置bind 0.0.0.0的话,默认是127.0.0.1。只有在虚拟机中才能连接到redis,也就是只能单纯的用命令行却set,get,你项目中永远无法连接到虚拟中的redis。

在这里插入图片描述
你就永远无法连接到192.168.58.129。

大佬留言

曙光(2430486030) 下午 7:28:34
你通过docker创建redis的时候,通过挂载的方式进行启动

曙光(2430486030) 下午 7:28:48
然后挂载的时候通过配置文件的方式启动就好了

曙光(2430486030) 下午 7:29:08
如果你要设置密码挂载的时候还要加上密码,配置文件里面也要改

在这里插入图片描述

异常

  1. 浏览器报错

Uncaught (in promise) Error: timeout of 5000ms exceeded
at createError (createError.js:16)
at XMLHttpRequest.handleTimeout (xhr.js:95)

解决:启用redis。

  1. org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.58.129:6379

解决:未连接到Redis。只要配置一次。一劳永逸。

docker start redis即可。

最终结果呈现:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值