Download node_modules/@distube/ytdl-core/lib/cache.js from sweet2004/bot: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/sweet2004/bot/resolve/main/node_modules/%40distube/ytdl-core/lib/cache.js
- Command line
-
hf download hf://spaces/sweet2004/bot/node_modules/@distube/ytdl-core/lib/cache.js
-
curl -L -o cache.js https://huggingface.co/spaces/sweet2004/bot/resolve/main/node_modules/%40distube/ytdl-core/lib/cache.js
1.06 kB
| const { setTimeout } = require('timers'); | |
| // A cache that expires. | |
| module.exports = class Cache extends Map { | |
| constructor(timeout = 1000) { | |
| super(); | |
| this.timeout = timeout; | |
| } | |
| set(key, value) { | |
| if (this.has(key)) { | |
| clearTimeout(super.get(key).tid); | |
| } | |
| super.set(key, { | |
| tid: setTimeout(this.delete.bind(this, key), this.timeout).unref(), | |
| value, | |
| }); | |
| } | |
| get(key) { | |
| let entry = super.get(key); | |
| if (entry) { | |
| return entry.value; | |
| } | |
| return null; | |
| } | |
| getOrSet(key, fn) { | |
| if (this.has(key)) { | |
| return this.get(key); | |
| } else { | |
| let value = fn(); | |
| this.set(key, value); | |
| (async() => { | |
| try { | |
| await value; | |
| } catch (err) { | |
| this.delete(key); | |
| } | |
| })(); | |
| return value; | |
| } | |
| } | |
| delete(key) { | |
| let entry = super.get(key); | |
| if (entry) { | |
| clearTimeout(entry.tid); | |
| super.delete(key); | |
| } | |
| } | |
| clear() { | |
| for (let entry of this.values()) { | |
| clearTimeout(entry.tid); | |
| } | |
| super.clear(); | |
| } | |
| }; | |