mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2025-10-22 22:53:09 +00:00

Refactored main.js to optimize module and server loading, improving code clarity and lazy loading. Updated several dependencies in package.json to their latest versions for better security and compatibility. Updated documentation cover page and home page to reflect project branding changes. Co-Authored-By: binaryify <binaryify@gmail.com>
72 lines
1.3 KiB
JavaScript
72 lines
1.3 KiB
JavaScript
class MemoryCache {
|
|
constructor() {
|
|
this.cache = new Map()
|
|
this.size = 0
|
|
}
|
|
|
|
add(key, value, time, timeoutCallback) {
|
|
// 移除旧的条目(如果存在)
|
|
const old = this.cache.get(key)
|
|
if (old) {
|
|
clearTimeout(old.timeout)
|
|
}
|
|
|
|
// 创建新的缓存条目
|
|
const entry = {
|
|
value,
|
|
expire: time + Date.now(),
|
|
timeout: setTimeout(() => {
|
|
this.delete(key)
|
|
if (typeof timeoutCallback === 'function') {
|
|
timeoutCallback(value, key)
|
|
}
|
|
}, time),
|
|
}
|
|
|
|
this.cache.set(key, entry)
|
|
this.size = this.cache.size
|
|
|
|
return entry
|
|
}
|
|
|
|
delete(key) {
|
|
const entry = this.cache.get(key)
|
|
if (entry) {
|
|
clearTimeout(entry.timeout)
|
|
this.cache.delete(key)
|
|
this.size = this.cache.size
|
|
}
|
|
return null
|
|
}
|
|
|
|
get(key) {
|
|
return this.cache.get(key) || null
|
|
}
|
|
|
|
getValue(key) {
|
|
const entry = this.cache.get(key)
|
|
return entry ? entry.value : undefined
|
|
}
|
|
|
|
clear() {
|
|
this.cache.forEach((entry) => clearTimeout(entry.timeout))
|
|
this.cache.clear()
|
|
this.size = 0
|
|
return true
|
|
}
|
|
|
|
has(key) {
|
|
const entry = this.cache.get(key)
|
|
if (!entry) return false
|
|
|
|
if (Date.now() > entry.expire) {
|
|
this.delete(key)
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
}
|
|
|
|
module.exports = MemoryCache
|