override fun getOrPut()

in dnq/src/main/kotlin/kotlinx/dnq/singleton/Caches.kt [35:46]


    override fun <XD : XdEntity> getOrPut(type: XdSingletonEntityType<XD>, findOrNew: () -> XD): XD {
        val cacheKey = type.cacheKey
        val cached = cache.tryKey(cacheKey)

        // with very small probability we can have a collision here
        if (cached != null && cached.store === type.storeContainer.store) {
            return cached.xdEntity as XD
        }
        val instance = findOrNew()
        cache.cacheObject(cacheKey, CachedValue(type.storeContainer.store, instance))
        return instance
    }