syntropylog
Version:
An instance manager with observability for Node.js applications
27 lines • 933 B
JavaScript
/**
* FILE: src/services/UserService.ts
* DESCRIPTION: An example service to demonstrate testing with BeaconRedisMock.
*/
// A mock database function to simulate fetching data from a primary source.
async function fetchUserFromDb(userId) {
// In a real app, this would be a database query.
return { id: userId, name: 'John Doe', email: 'john.doe@example.com' };
}
export class UserService {
redis;
userCacheTtl = 3600; // 1 hour
constructor(redisClient) {
this.redis = redisClient;
}
async getUserById(userId) {
const cacheKey = `user:${userId}`;
const cachedUser = await this.redis.get(cacheKey);
if (cachedUser) {
return JSON.parse(cachedUser);
}
const user = await fetchUserFromDb(userId);
await this.redis.set(cacheKey, JSON.stringify(user), this.userCacheTtl);
return user;
}
}
//# sourceMappingURL=UserService.js.map