增加定时任务更新最后活跃时间

This commit is contained in:
2023-12-01 21:51:07 +08:00
parent 4950578310
commit 51ac3f8ff8
6 changed files with 36 additions and 0 deletions

View File

@@ -17,5 +17,6 @@ public class CreateLoginUserDto extends CommonDto {
private String captcha;
private Integer timeout;
private LocalDateTime loginTime;
private LocalDateTime lastActiveTime;
private LocalDateTime expireTime;
}

View File

@@ -19,5 +19,6 @@ public class LoginUserDto extends CommonDto {
private Integer timeout;
private LocalDateTime loginTime;
private LocalDateTime lastActiveTime;
private LocalDateTime expireTime;
}

View File

@@ -18,5 +18,6 @@ public class UpdateLoginUserDto extends CommonDto {
private Integer timeout;
private LocalDateTime loginTime;
private LocalDateTime lastActiveTime;
private LocalDateTime expireTime;
}

View File

@@ -23,5 +23,6 @@ public class LoginUserEntity extends CommonEntity {
private Integer timeout;
private LocalDateTime loginTime;
private LocalDateTime lastActiveTime;
private LocalDateTime expireTime;
}

View File

@@ -14,6 +14,13 @@ import java.util.List;
@Repository
public interface LoginUserRepository extends BaseRepository<LoginUserEntity> {
@Transactional
@Modifying
@Query(
"update LoginUserEntity l set l.lastActiveTime = ?1 where l.id = ?2 and l.lastActiveTime < ?3")
int updateLastActiveTimeByIdAndLastActiveTimeLessThan(
LocalDateTime lastActiveTime, String id, LocalDateTime lastActiveTime1);
@Transactional
@Modifying
@Query("update LoginUserEntity l set l.expireTime = ?1 where l.id = ?2")

View File

@@ -17,6 +17,8 @@ import cn.lihongjie.coal.session.SessionService;
import cn.lihongjie.coal.sysconfig.service.SysConfigService;
import cn.lihongjie.coal.user.service.UserService;
import com.google.common.collect.Maps;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
@@ -38,7 +40,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -57,12 +61,15 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
@Autowired private ConversionService conversionService;
private ScheduledExecutorService executorService;
private final Map<String, LocalDateTime> lastActivateTime = Maps.newConcurrentMap();
@PostConstruct
public void init() {
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(this::deleteExpireSession, 1, 1, TimeUnit.MINUTES);
executorService.scheduleAtFixedRate(this::syncLastActivateTime, 1, 1, TimeUnit.MINUTES);
}
@SneakyThrows
@@ -160,8 +167,26 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
}
}
private void syncLastActivateTime() {
HashMap<String, LocalDateTime> copy = new HashMap<>(lastActivateTime);
lastActivateTime.clear();
for (Map.Entry<String, LocalDateTime> entry : copy.entrySet()) {
repository.updateLastActiveTimeByIdAndLastActiveTimeLessThan(
entry.getValue(), entry.getKey(), entry.getValue());
log.info("更新会话最后活跃时间: {} to {}", entry.getKey(), entry.getValue());
}
}
public void touch(LoginUserDto loginDto) {
lastActivateTime.put(loginDto.getId(), LocalDateTime.now());
Integer timeout = loginDto.getTimeout();
LocalDateTime expireTime = loginDto.getExpireTime();