Skip to content

Commit

Permalink
fix: 数据库点击记录插入冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 27, 2023
1 parent 032ce0f commit 6af00a4
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions app/src/main/kotlin/li/songe/gkd/service/AbState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package li.songe.gkd.service

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import li.songe.gkd.appScope
import li.songe.gkd.data.AppRule
import li.songe.gkd.data.ClickLog
Expand Down Expand Up @@ -102,25 +104,28 @@ var lastTriggerRule: ResolvedRule? = null
var lastTriggerTime = 0L
var appChangeTime = 0L

val clickLogMutex = Mutex()
fun insertClickLog(rule: ResolvedRule) {
appScope.launchTry(Dispatchers.IO) {
val clickLog = ClickLog(
appId = topActivityFlow.value.appId,
activityId = topActivityFlow.value.activityId,
subsId = rule.subsItem.id,
subsVersion = rule.rawSubs.version,
groupKey = rule.group.key,
groupType = when (rule) {
is AppRule -> SubsConfig.AppGroupType
is GlobalRule -> SubsConfig.GlobalGroupType
},
ruleIndex = rule.index,
ruleKey = rule.key,
)
DbSet.clickLogDao.insert(clickLog)
increaseClickCount()
if (recordStoreFlow.value.clickCount % 100 == 0) {
DbSet.clickLogDao.deleteKeepLatest()
clickLogMutex.withLock {
increaseClickCount()
val clickLog = ClickLog(
appId = topActivityFlow.value.appId,
activityId = topActivityFlow.value.activityId,
subsId = rule.subsItem.id,
subsVersion = rule.rawSubs.version,
groupKey = rule.group.key,
groupType = when (rule) {
is AppRule -> SubsConfig.AppGroupType
is GlobalRule -> SubsConfig.GlobalGroupType
},
ruleIndex = rule.index,
ruleKey = rule.key,
)
DbSet.clickLogDao.insert(clickLog)
if (recordStoreFlow.value.clickCount % 100 == 0) {
DbSet.clickLogDao.deleteKeepLatest()
}
}
}
}

0 comments on commit 6af00a4

Please sign in to comment.