mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-19 09:41:33 +00:00
feat: SimpleDataStore.hold() no longer use up size
this means internal Map-store may actually contain more keys than limit, but only if the extraenous keys are held-keys (i.e. has null value) this expects that you don't manually set keys with null values into the store however
This commit is contained in:
parent
617dc6a056
commit
17c863f724
@ -5,6 +5,7 @@ const STRATEGIES = [
|
|||||||
|
|
||||||
class SimpleDataStore {
|
class SimpleDataStore {
|
||||||
#store
|
#store
|
||||||
|
#size
|
||||||
#limit
|
#limit
|
||||||
#strategy
|
#strategy
|
||||||
|
|
||||||
@ -22,22 +23,28 @@ class SimpleDataStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.#store = new Map()
|
this.#store = new Map()
|
||||||
|
this.#size = this.#store.size
|
||||||
this.#limit = options.limit
|
this.#limit = options.limit
|
||||||
this.#strategy = options.strategy
|
this.#strategy = options.strategy
|
||||||
}
|
}
|
||||||
|
|
||||||
clear () {
|
clear () {
|
||||||
return this.#store.clear()
|
this.#store.clear()
|
||||||
|
this.#size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
delete (key) {
|
delete (key) {
|
||||||
return this.#store.delete(key)
|
if (this.#store.delete(key)) {
|
||||||
|
this.#size--
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteStalest () {
|
deleteStalest () {
|
||||||
const stalest = this.getStalest()
|
const stalest = this.getStalest()
|
||||||
if (stalest) {
|
if (stalest) {
|
||||||
return this.#store.delete(stalest)
|
return this.delete(stalest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,15 +88,12 @@ class SimpleDataStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hold (key) {
|
hold (key) {
|
||||||
if (this.#store.size >= this.#limit) {
|
this.#store.set(key, null)
|
||||||
this.deleteStalest()
|
return true
|
||||||
}
|
|
||||||
return this.#store.set(key, null) && true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
set (key, value) {
|
set (key, value) {
|
||||||
// Only do deleteStalest() if this key legitimately had not been set or held via hold()
|
if (this.#size >= this.#limit) {
|
||||||
if (this.#store.get(key) === undefined && this.#store.size >= this.#limit) {
|
|
||||||
this.deleteStalest()
|
this.deleteStalest()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +107,11 @@ class SimpleDataStore {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.#store.set(key, { value, stratval }) && true
|
if (this.#store.set(key, { value, stratval })) {
|
||||||
|
this.#size++
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
get limit () {
|
get limit () {
|
||||||
@ -115,7 +123,7 @@ class SimpleDataStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get size () {
|
get size () {
|
||||||
return this.#store.size
|
return this.#size
|
||||||
}
|
}
|
||||||
|
|
||||||
set size (_) {
|
set size (_) {
|
||||||
|
Loading…
Reference in New Issue
Block a user