[CLI] free cached values

^ KTIJ-24447
This commit is contained in:
Anna Kozlova
2023-01-31 10:03:16 +01:00
committed by Space Team
parent abf70a586c
commit 30fd34c2d7
2 changed files with 16 additions and 0 deletions
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.analysis.api.impl.base
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.openapi.util.LowMemoryWatcher
import com.intellij.psi.util.PsiModificationTracker
import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
@@ -61,6 +62,10 @@ private class KtAnalysisSessionCache(project: Project) {
)
)
init {
LowMemoryWatcher.register({ cache.clearCachedValues() }, project)
}
@TestOnly
fun clear() {
cache.clear()
@@ -9,6 +9,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
import com.intellij.util.CachedValueBase
import com.intellij.util.containers.ContainerUtil
import java.util.concurrent.ConcurrentHashMap
@@ -17,6 +18,8 @@ public abstract class SoftCachedMap<K : Any, V : Any> {
public abstract fun clear()
public abstract fun clearCachedValues()
public companion object {
public fun <K : Any, V : Any> create(
project: Project,
@@ -48,6 +51,12 @@ private class SoftCachedMapWithTrackers<K : Any, V : Any>(
cache.clear()
}
override fun clearCachedValues() {
cache.values.forEach {
(it as? CachedValueBase<*>)?.clear()
}
}
override fun getOrPut(key: K, create: () -> V): V {
return cache.getOrPut(key) {
CachedValuesManager.getManager(project).createCachedValue {
@@ -67,6 +76,8 @@ private class SoftCachedMapWithoutTrackers<K : Any, V : Any>(kind: Kind) : SoftC
cache.clear()
}
override fun clearCachedValues() {}
override fun getOrPut(key: K, create: () -> V): V {
return cache.getOrPut(key, create)
}