-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample.js
More file actions
43 lines (35 loc) · 957 Bytes
/
Copy pathsample.js
File metadata and controls
43 lines (35 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class UserService {
constructor(database) {
this.db = database;
this.cache = new Cache();
}
async getUser(id) {
// Try cache first
const cached = await this.cache.get(id);
if (cached) {
return cached;
}
// Get from database
const user = await this.db.users.findOne(id);
if (!user) {
throw new Error('User not found');
}
// Update cache
await this.cache.set(id, user);
return user;
}
async updateUser(id, data) {
// Validate input
if (!this.validateUserData(data)) {
throw new Error('Invalid user data');
}
// Update in database
const updated = await this.db.users.update(id, data);
// Clear cache
await this.cache.delete(id);
return updated;
}
validateUserData(data) {
return data.name && data.email;
}
}