-
Notifications
You must be signed in to change notification settings - Fork 35
/
ClanService.js
42 lines (33 loc) · 1.12 KB
/
ClanService.js
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
const { MutexService } = require('./MutexService')
const clanTTL = 60 * 5
class ClanService {
constructor (cacheService, dataRepository, lockTimeout = 3000) {
this.lockTimeout = lockTimeout
this.cacheService = cacheService
this.mutexService = new MutexService()
this.dataRepository = dataRepository
}
getCacheKey (name) {
return 'ClanService_' + name
}
async getClan (id, ignoreCache = false) {
return await this.dataRepository.fetchClan(id)
}
async getAll () {
const cacheKey = this.getCacheKey('all')
if (this.cacheService.has(cacheKey)) {
return this.cacheService.get(cacheKey)
}
if (this.mutexService.locked) {
await this.mutexService.acquire(() => {
}, this.lockTimeout)
return this.getAll()
}
await this.mutexService.acquire(async () => {
const result = await this.dataRepository.fetchAllClans()
this.cacheService.set(cacheKey, result, clanTTL)
})
return this.getAll()
}
}
module.exports.ClanService = ClanService