fun putIfNotCached()

in entity-store/src/main/kotlin/jetbrains/exodus/entitystore/EntityIterableCache.kt [101:144]


    fun putIfNotCached(it: EntityIterableBase): EntityIterableBase {
        if (cachingDisabled || !it.canBeCached()) {
            return it
        }
        val handle = it.handle
        val txn = it.transaction
        val localCache = txn.localCache
        txn.localCacheAttempt()
        val cached: EntityIterableBase? = localCache.getObject(handle)
        if (cached != null) {
            if (!cached.handle.isExpired) {
                txn.localCacheHit()
                stats.incTotalHits()
                return cached
            }
            localCache.remove(handle)
        }
        stats.incTotalMisses()
        if (txn.isMutable || !txn.isCurrent || !txn.isCachingRelevant) {
            return it
        }

        // if cache is enough full, then cache iterables after they live some time in deferred cache
        if (config.entityIterableCacheDeferredEnabled && localCache.halfFull) {
            val currentMillis = System.currentTimeMillis()
            val handleIdentity = handle.identity
            val whenCached = deferredIterablesCache.tryKey(handleIdentity)
            if (whenCached == null) {
                deferredIterablesCache.cacheObject(handleIdentity, currentMillis)
                return it
            }
            if (whenCached + config.entityIterableCacheDeferredDelay > currentMillis) {
                return it
            }
        }

        // if we are already within caching dispatcher,
        // then instantiate iterable without queueing a job.
        if (isDispatcherThread) {
            return it.getOrCreateCachedInstance(txn)
        }
        EntityIterableAsyncInstantiation(handle, it, true, processor)
        return it
    }