forked from hannibal002/SkyHanni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creating and using TimeLimitedCache and TimeLimitedSet instead of gua…
…va cache.
- Loading branch information
1 parent
dd86bba
commit 680b203
Showing
9 changed files
with
63 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package at.hannibal2.skyhanni.utils | ||
|
||
import com.google.common.cache.CacheBuilder | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.time.Duration | ||
|
||
class TimeLimitedCache<K, V>(expireAfterWrite: Duration) { | ||
|
||
private val cache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS).build<K, V>() | ||
|
||
fun put(key: K, value: V) = cache.put(key, value) | ||
|
||
fun getOrNull(key: K): V? = cache.getIfPresent(key) | ||
|
||
fun clear() = cache.invalidateAll() | ||
|
||
fun values(): MutableCollection<V> = cache.asMap().values | ||
|
||
fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package at.hannibal2.skyhanni.utils | ||
|
||
import kotlin.time.Duration | ||
|
||
class TimeLimitedSet<T>(expireAfterWrite: Duration) { | ||
|
||
private val cache = TimeLimitedCache<T, Unit>(expireAfterWrite) | ||
|
||
fun add(element: T) = cache.put(element, Unit) | ||
|
||
fun contains(element: T): Boolean = cache.containsKey(element) | ||
|
||
fun clear() = cache.clear() | ||
} |