面试题答案
一键面试运用Node.js模块化机制实现低耦合、高内聚
- 按模块划分文件:
- 将每个业务模块(用户模块、商品模块、订单模块等)的代码分别放在独立的文件或文件夹中。例如,对于用户模块,可以创建
user.js
文件或user
文件夹,在其中编写用户相关的业务逻辑和数据库操作代码。 - 以用户模块为例,
user.js
文件可能包含用户注册、登录、信息查询等功能的实现。
- 将每个业务模块(用户模块、商品模块、订单模块等)的代码分别放在独立的文件或文件夹中。例如,对于用户模块,可以创建
// user.js
const User = require('../models/user'); // 引入用户模型
exports.register = async (userData) => {
try {
const newUser = new User(userData);
await newUser.save();
return newUser;
} catch (error) {
throw error;
}
};
exports.login = async (credentials) => {
// 实现登录逻辑
};
- 使用
exports
或module.exports
暴露接口:- 在每个模块文件中,通过
exports
或module.exports
暴露模块对外的接口。只暴露必要的函数、对象或变量,避免暴露内部实现细节,从而实现高内聚。 - 如上述
user.js
文件,通过exports
暴露了register
和login
两个接口,其他模块只需调用这些接口,无需了解具体实现。
- 在每个模块文件中,通过
- 模块间依赖管理:
- 在需要使用其他模块功能的地方,通过
require
引入相应模块。例如,在订单模块中,如果需要验证下单用户的信息,可以引入用户模块。
- 在需要使用其他模块功能的地方,通过
// order.js
const userModule = require('./user');
exports.placeOrder = async (orderData) => {
const user = await userModule.getUserById(orderData.userId);
// 进行订单相关操作
};
- 这种方式使得每个模块只依赖其明确需要的其他模块,降低了模块之间的耦合度。
利用模块化管理缓存提高系统性能
- 模块级缓存:
- 在每个模块内部,可以实现局部缓存。例如,对于商品模块中经常查询的热门商品列表,可以在模块内设置一个缓存变量。
// product.js
let popularProductsCache;
exports.getPopularProducts = async () => {
if (popularProductsCache) {
return popularProductsCache;
}
const products = await Product.find({ isPopular: true });
popularProductsCache = products;
return products;
};
- 这样,在同一模块内多次调用该函数时,若缓存存在则直接返回缓存数据,减少数据库查询次数,提高性能。
2. 共享缓存模块:
- 创建一个专门的缓存管理模块,用于管理全局或跨模块的缓存。该模块可以使用第三方缓存库,如 node-cache
。
// cache.js
const NodeCache = require('node-cache');
const cache = new NodeCache();
exports.set = (key, value, ttl = 60) => {
cache.set(key, value, ttl);
};
exports.get = (key) => {
return cache.get(key);
};
- 各个业务模块可以通过引入该缓存模块来使用缓存功能。例如,订单模块在查询订单详情时,可以先尝试从缓存中获取数据。
// order.js
const cache = require('./cache');
exports.getOrderDetails = async (orderId) => {
let order = cache.get(`order:${orderId}`);
if (!order) {
order = await Order.findById(orderId);
cache.set(`order:${orderId}`, order, 300); // 缓存5分钟
}
return order;
};
- 缓存更新策略:
- 在进行数据更新操作时,要及时更新相应的缓存。例如,当用户修改个人信息时,不仅要更新数据库中的用户数据,还要清除或更新用户相关的缓存。
// user.js
const cache = require('./cache');
exports.updateUser = async (userId, updatedData) => {
const user = await User.findByIdAndUpdate(userId, updatedData, { new: true });
cache.del(`user:${userId}`); // 清除用户缓存
return user;
};
- 这样确保缓存数据与数据库数据的一致性,避免返回过时的数据。通过合理运用模块化的缓存管理,可以有效减少数据库负载,提高系统整体性能。