mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-09 19:32:40 +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,
|
builder: (key: K) -> V,
|
||||||
): V {
|
): V {
|
||||||
val softRef = cache[key]
|
val softRef = cache[key]
|
||||||
val value = softRef?.get()
|
|
||||||
|
|
||||||
return if (value != null) {
|
return if (softRef == null) {
|
||||||
value
|
|
||||||
} else {
|
|
||||||
val newObject = builder(key)
|
val newObject = builder(key)
|
||||||
cache.putIfAbsent(key, WeakReference(newObject))?.get() ?: newObject
|
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