连接redis
docker ps 查看redis的pid
- docker exec -it c74a901d8026 bash
- redis-cli
- get *
- set a b
- 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
如果你要设置密码挂载的时候还要加上密码,配置文件里面也要改
异常
- 浏览器报错
Uncaught (in promise) Error: timeout of 5000ms exceeded
at createError (createError.js:16)
at XMLHttpRequest.handleTimeout (xhr.js:95)
解决:启用redis。
- 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即可。
最终结果呈现: