From 9996b9373d9268310f37e61a2e2b57175a151061 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Fri, 15 Mar 2024 15:51:40 -0400 Subject: [PATCH] Allow resetting a subset --- .../kotlin/com/kylecorry/luna/cache/Hooks.kt | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/kylecorry/luna/cache/Hooks.kt b/src/main/kotlin/com/kylecorry/luna/cache/Hooks.kt index 5132ff4..4d2965d 100644 --- a/src/main/kotlin/com/kylecorry/luna/cache/Hooks.kt +++ b/src/main/kotlin/com/kylecorry/luna/cache/Hooks.kt @@ -36,15 +36,33 @@ class Hooks { return memo.getOrPut(*values, value = value) } - fun resetEffects(except: List = emptyList()) { + /** + * Resets effects + * @param keys The keys to reset, if null all effects are reset + * @param except The keys to not reset + */ + fun resetEffects(keys: List? = null, except: List = emptyList()) { synchronized(effectLock) { - effects.keys.removeAll { it !in except } + if (keys == null) { + effects.keys.removeAll { it !in except } + } else { + effects.keys.removeAll { it !in except && it in keys } + } } } - fun resetMemos(except: List = emptyList()) { + /** + * Resets memos + * @param keys The keys to reset, if null all memos are reset + * @param except The keys to not reset + */ + fun resetMemos(keys: List? = null, except: List = emptyList()) { synchronized(memoLock) { - memos.keys.removeAll { it !in except } + if (keys == null) { + memos.keys.removeAll { it !in except } + } else { + memos.keys.removeAll { it !in except && it in keys } + } } }