Miao-Yunzai/src/mys/BaseModel.ts

104 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-06-09 01:00:07 +08:00
/**
*
*/
import MysUtil from './MysUtil.js'
let cacheMap = {}
let reFn = {}
export default class BaseModel {
2024-06-09 01:00:25 +08:00
constructor() {
2024-06-09 01:00:07 +08:00
return this
}
2024-06-09 11:40:24 +08:00
/**
*
* @param model
* @param id
* @param time
* @returns
*/
2024-06-09 01:00:25 +08:00
_getThis(model, id = '', time = 10 * 60) {
2024-06-09 01:00:07 +08:00
const uuid = `${model}:${id}`
this._uuid = uuid
if (uuid && cacheMap[uuid]) {
return cacheMap[uuid]._expire(time)
}
}
2024-06-09 11:40:24 +08:00
/**
*
* @param model
* @param id
* @param time
* @returns
*/
2024-06-09 01:00:25 +08:00
_cacheThis(model, id, time = 10 * 60) {
2024-06-09 01:00:07 +08:00
const uuid = this._uuid || `${model}:${id}`
this._uuid = uuid
if (uuid) {
this._expire(time)
cacheMap[uuid] = this
return cacheMap[uuid]
}
return this
}
2024-06-09 11:40:24 +08:00
/**
*
* @param time
* @returns
*/
2024-06-09 01:00:25 +08:00
_expire(time = 10 * 60) {
2024-06-09 01:00:07 +08:00
let id = this._uuid
reFn[id] && clearTimeout(reFn[id])
if (time > 0) {
if (id) {
reFn[id] = setTimeout(() => {
reFn[id] && clearTimeout(reFn[id])
delete reFn[id]
delete cacheMap[id]
}, time * 1000)
}
return cacheMap[id]
}
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
_delCache() {
2024-06-09 01:00:07 +08:00
let id = this._uuid
reFn[id] && clearTimeout(reFn[id])
delete reFn[id]
delete cacheMap[id]
}
2024-06-09 11:40:24 +08:00
/**
*
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
gameKey(game = 'gs') {
2024-06-09 01:00:07 +08:00
return MysUtil.getGameKey(game)
}
2024-06-09 11:40:24 +08:00
/**
*
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
isGs(game = 'gs') {
2024-06-09 01:00:07 +08:00
return this.gameKey(game) === 'gs'
}
2024-06-09 11:40:24 +08:00
/**
*
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
isSr(game = 'gs') {
2024-06-09 01:00:07 +08:00
return this.gameKey(game) === 'sr'
}
}