mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
增加定时任务更新最后活跃时间
This commit is contained in:
@@ -17,5 +17,6 @@ public class CreateLoginUserDto extends CommonDto {
|
||||
private String captcha;
|
||||
private Integer timeout;
|
||||
private LocalDateTime loginTime;
|
||||
private LocalDateTime lastActiveTime;
|
||||
private LocalDateTime expireTime;
|
||||
}
|
||||
|
||||
@@ -19,5 +19,6 @@ public class LoginUserDto extends CommonDto {
|
||||
|
||||
private Integer timeout;
|
||||
private LocalDateTime loginTime;
|
||||
private LocalDateTime lastActiveTime;
|
||||
private LocalDateTime expireTime;
|
||||
}
|
||||
|
||||
@@ -18,5 +18,6 @@ public class UpdateLoginUserDto extends CommonDto {
|
||||
|
||||
private Integer timeout;
|
||||
private LocalDateTime loginTime;
|
||||
private LocalDateTime lastActiveTime;
|
||||
private LocalDateTime expireTime;
|
||||
}
|
||||
|
||||
@@ -23,5 +23,6 @@ public class LoginUserEntity extends CommonEntity {
|
||||
|
||||
private Integer timeout;
|
||||
private LocalDateTime loginTime;
|
||||
private LocalDateTime lastActiveTime;
|
||||
private LocalDateTime expireTime;
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user