node.js - Node相关问题,if (cache[absPath]) 语句不懂
巴扎黑
巴扎黑 2017-04-17 14:33:35
[Node.js讨论组]
// 提供静态文件服务
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里面的表达式怎么理解呢,最好详细,谢谢

巴扎黑
巴扎黑

全部回复(1)
怪我咯

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号