synchronize on cache object

This commit is contained in:
Milos Kozak 2021-10-19 21:27:44 +02:00
parent 391cbb575c
commit 674909fdd0

View file

@ -38,7 +38,7 @@ class ProfileFunctionImplementation @Inject constructor(
private val dateUtil: DateUtil,
private val config: Config,
private val hardLimits: HardLimits,
private val aapsSchedulers: AapsSchedulers,
aapsSchedulers: AapsSchedulers,
private val fabricPrivacy: FabricPrivacy
) : ProfileFunction {
@ -51,8 +51,8 @@ class ProfileFunctionImplementation @Inject constructor(
.toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe(
@Synchronized
{
synchronized(cache) {
for (index in cache.size() - 1 downTo 0) {
if (cache.keyAt(index) > it.startDate) {
aapsLogger.debug(LTag.AUTOSENS, "Removing from profileCache: " + dateUtil.dateAndTimeAndSecondsString(cache.keyAt(index)))
@ -61,6 +61,7 @@ class ProfileFunctionImplementation @Inject constructor(
break
}
}
}
}, fabricPrivacy::logException
)
}
@ -92,24 +93,24 @@ class ProfileFunctionImplementation @Inject constructor(
override fun getProfile(): Profile? =
getProfile(dateUtil.now())
@Synchronized
override fun getProfile(time: Long): Profile? {
val rounded = time - time % 1000
// Clear cache after longer use
synchronized(cache) {
if (cache.size() > 30000) {
cache.clear()
aapsLogger.debug("Profile cache cleared")
}
val rounded = time - time % 1000
val cached = cache[rounded]
if (cached != null) {
// aapsLogger.debug("HIT getProfile for $time $rounded")
return cached
if (cached != null) return cached
}
// aapsLogger.debug("getProfile called for $time")
val ps = repository.getEffectiveProfileSwitchActiveAt(time).blockingGet()
if (ps is ValueWrapper.Existing) {
val sealed = ProfileSealed.EPS(ps.value)
synchronized(cache) {
cache.put(rounded, sealed)
}
return sealed
}
return null