演武场和练兵场
This commit is contained in:
36
game-server/app/services/redlockCacheService.ts
Normal file
36
game-server/app/services/redlockCacheService.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { fromCallback } from 'bluebird';
|
||||
import { scheduleJob } from 'node-schedule';
|
||||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { RedlockService } from '../services/redLockService';
|
||||
interface UserCache {
|
||||
time: number;
|
||||
lock: any;
|
||||
}
|
||||
var userCacheMap = new Map<string, UserCache>();
|
||||
|
||||
export function init() {
|
||||
scheduleJob("0/5 * * * * *", clearDirtyData, {name:'clearDirtyData'});
|
||||
}
|
||||
|
||||
export function clearDirtyData() {
|
||||
userCacheMap.forEach(function(userCache, key) {
|
||||
if(nowSeconds() > userCache.time + 10){
|
||||
console.log('show lock =' + JSON.stringify(userCache.lock));
|
||||
userCacheMap.delete(key);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function releaseLock(lockKey: string) {
|
||||
var userCache = userCacheMap.get(lockKey);
|
||||
if (!!userCache && userCache.lock){
|
||||
// unlock your resource when you are done
|
||||
userCache.lock.unlock();
|
||||
}
|
||||
userCacheMap.delete(lockKey);
|
||||
}
|
||||
|
||||
|
||||
export function setLock(lockKey: string, lock: any){
|
||||
userCacheMap.set(lockKey, {lock, time: nowSeconds()})
|
||||
};
|
||||
Reference in New Issue
Block a user