2023-03-04 14:30:13 +08:00
|
|
|
|
/**
|
|
|
|
|
* 基础类,提供实例缓存等一些基础方法
|
|
|
|
|
*/
|
2023-05-09 11:03:38 +08:00
|
|
|
|
import MysUtil from './MysUtil.js'
|
|
|
|
|
|
2023-03-04 14:30:13 +08:00
|
|
|
|
let cacheMap = {}
|
|
|
|
|
let reFn = {}
|
|
|
|
|
|
|
|
|
|
export default class BaseModel {
|
|
|
|
|
constructor () {
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取缓存
|
|
|
|
|
_getThis (model, id = '', time = 10 * 60) {
|
|
|
|
|
const uuid = `${model}:${id}`
|
2023-05-08 05:11:29 +08:00
|
|
|
|
this._uuid = uuid
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (uuid && cacheMap[uuid]) {
|
|
|
|
|
return cacheMap[uuid]._expire(time)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置缓存
|
2023-05-08 05:11:29 +08:00
|
|
|
|
_cacheThis (model, id, time = 10 * 60) {
|
|
|
|
|
const uuid = this._uuid || `${model}:${id}`
|
|
|
|
|
this._uuid = uuid
|
|
|
|
|
if (uuid) {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
this._expire(time)
|
2023-05-08 05:11:29 +08:00
|
|
|
|
cacheMap[uuid] = this
|
|
|
|
|
return cacheMap[uuid]
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置超时时间
|
|
|
|
|
_expire (time = 10 * 60) {
|
|
|
|
|
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]
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-08 05:11:29 +08:00
|
|
|
|
|
|
|
|
|
_delCache () {
|
|
|
|
|
let id = this._uuid
|
|
|
|
|
reFn[id] && clearTimeout(reFn[id])
|
|
|
|
|
delete reFn[id]
|
|
|
|
|
delete cacheMap[id]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gameKey (game = 'gs') {
|
2023-05-09 11:03:38 +08:00
|
|
|
|
return MysUtil.getGameKey(game)
|
2023-05-08 05:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isGs (game = 'gs') {
|
|
|
|
|
return this.gameKey(game) === 'gs'
|
|
|
|
|
}
|
2023-05-09 11:03:38 +08:00
|
|
|
|
|
|
|
|
|
isSr (game = 'gs') {
|
|
|
|
|
return this.gameKey(game) === 'sr'
|
|
|
|
|
}
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|