mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-28 13:06:47 +02:00
Fixes the unable to recreate user after the GC deletes it.
This commit is contained in:
@@ -85,13 +85,21 @@ class LargeSoftCache<K, V> : CacheOperations<K, V> {
|
||||
builder: (key: K) -> V,
|
||||
): V {
|
||||
val softRef = cache[key]
|
||||
val value = softRef?.get()
|
||||
|
||||
return if (value != null) {
|
||||
value
|
||||
} else {
|
||||
return if (softRef == null) {
|
||||
val newObject = builder(key)
|
||||
cache.putIfAbsent(key, WeakReference(newObject))?.get() ?: newObject
|
||||
} else {
|
||||
val value = softRef.get()
|
||||
if (value != null) {
|
||||
value
|
||||
} else {
|
||||
// removes first to make sure the putIfAbsent works.
|
||||
// another thread may put in between
|
||||
cache.remove(key, softRef)
|
||||
val newObject = builder(key)
|
||||
return cache.putIfAbsent(key, WeakReference(newObject))?.get() ?: newObject
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user