mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-02-22 13:19:05 +00:00
feat: SimpleDataStore allow pre-holding key
This commit is contained in:
parent
d31181b4be
commit
ecb30cd159
@ -34,9 +34,19 @@ class SimpleDataStore {
|
|||||||
return this.#store.delete(key)
|
return this.#store.delete(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deleteStalest () {
|
||||||
|
const stalest = this.getStalest()
|
||||||
|
if (stalest) {
|
||||||
|
return this.#store.delete(stalest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get (key) {
|
get (key) {
|
||||||
const entry = this.#store.get(key)
|
const entry = this.#store.get(key)
|
||||||
if (typeof entry === 'undefined') return entry
|
// This may return undefined or null
|
||||||
|
// undefined should be an indicator for when the key legitimately has not been set,
|
||||||
|
// null should be an indicator for when the key is still being held via hold() function
|
||||||
|
if (!entry) return entry
|
||||||
|
|
||||||
switch (this.#strategy) {
|
switch (this.#strategy) {
|
||||||
case STRATEGIES[0]:
|
case STRATEGIES[0]:
|
||||||
@ -59,7 +69,7 @@ class SimpleDataStore {
|
|||||||
case STRATEGIES[0]:
|
case STRATEGIES[0]:
|
||||||
case STRATEGIES[1]:
|
case STRATEGIES[1]:
|
||||||
for (const entry of this.#store) {
|
for (const entry of this.#store) {
|
||||||
if (entry[1].stratval < stalest[1].stratval) {
|
if (entry[1] && entry[1].stratval < stalest[1].stratval) {
|
||||||
stalest = entry
|
stalest = entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,12 +80,17 @@ class SimpleDataStore {
|
|||||||
return stalest[0]
|
return stalest[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
set (key, value) {
|
hold (key) {
|
||||||
if (this.#store.size >= this.#limit) {
|
if (this.#store.size >= this.#limit) {
|
||||||
const stalest = this.getStalest()
|
this.deleteStalest()
|
||||||
if (stalest) {
|
|
||||||
this.#store.delete(stalest)
|
|
||||||
}
|
}
|
||||||
|
return this.#store.set(key, null) && true
|
||||||
|
}
|
||||||
|
|
||||||
|
set (key, value) {
|
||||||
|
// Only do deleteStalest() if this key legitimately had not been set or held via hold()
|
||||||
|
if (this.#store.get(key) === undefined && this.#store.size >= this.#limit) {
|
||||||
|
this.deleteStalest()
|
||||||
}
|
}
|
||||||
|
|
||||||
let stratval
|
let stratval
|
||||||
|
Loading…
Reference in New Issue
Block a user