Node相关问题,if (cache[absPath]) 语句不懂

// 提供静态文件服务
function serverStatic(response, cache, absPath) {
    // 检查文件是否缓存在内存中
    if (cache[absPath]) {
        // 从内存中返回文件
        sendFile(response, absPath, cache[absPath]);
    } else {
        // 检查文件是否存在
        fs.exist(absPath, function (exist) {
            if (exist) {
                // 从硬盘中读取文件
                fs.readFile(absPath, function (err, data) {
                    if (err) {
                        send404(response);
                    } else {
                        cache[absPath] = data;
                        // 从硬盘中读取文件并返回
                        sendFile(response, absPath, data);
                    }
                });
            } else {
                // 返回404响应
                send404(response);
            }
        });
    }
}

想问的是:if (cache[absPath]) 这句话怎么理解,cache 是空对象{},absPath是绝对路径,cache()我查了没查到具体的解释, if里面的表达式怎么理解呢,最好详细,谢谢

阅读 2.6k
1 个回答

cache是个hash map, abspath作为key,先看看有没有cache,有就返回cache了的数据,不行就只能尝试从外部存储读数据返回,顺便放到cache里。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题