Miao-Yunzai/plugins/genshin/model/mys/BaseModel.js

70 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

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}`
this._uuid = uuid
2023-03-04 14:30:13 +08:00
if (uuid && cacheMap[uuid]) {
return cacheMap[uuid]._expire(time)
}
}
// 设置缓存
_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)
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]
}
}
_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)
}
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
}