big ass refactor to handle loading guilds and users without spring to make it more futureproof

This commit is contained in:
Lee
2024-07-01 01:12:32 +01:00
parent f566c3bcb5
commit d372c41c98
58 changed files with 755 additions and 638 deletions

View File

@ -1,73 +1,70 @@
package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils;
import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.repository.UserRepository;
import com.mongodb.client.model.Filters;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import net.jodah.expiringmap.ExpiringMap;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* @author Fascinated (fascinated7)
*/
@Service
@Log4j2
@Log4j2(topic = "User Service")
@Getter
@DependsOn("discordService")
@DependsOn({"discordService", "mongoService"})
public class UserService {
private static final long SAVE_INTERVAL = TimeUnit.MINUTES.toMillis(5);
/**
* The cached users
*/
private final Map<String, BatUser> users = ExpiringMap.builder()
.expiration(6, TimeUnit.HOURS)
.build();
/**
* The user repository to use
*/
private final UserRepository userRepository;
private final Map<String, BatUser> users = new HashMap<>();
@Autowired
public UserService(@NonNull UserRepository userRepository) {
this.userRepository = userRepository;
public UserService() {
TimerUtils.scheduleRepeating(() -> {
long before = System.currentTimeMillis();
for (BatUser user : users.values()) {
user.save();
}
log.info("Saved {} users in {}ms", users.size(), System.currentTimeMillis() - before);
}, SAVE_INTERVAL, SAVE_INTERVAL);
}
/**
* Gets a user by their ID
* Gets a user by its ID
*
* @param id The ID of the user
* @return The user
*/
public BatUser getUser(@NonNull String id) {
long before = System.currentTimeMillis();
// User is cached
if (users.containsKey(id)) {
return users.get(id);
}
long start = System.currentTimeMillis();
Optional<BatUser> optionalUser = userRepository.findById(id);
if (optionalUser.isPresent()) {
BatUser user = optionalUser.get();
// User is not cached
Document document = MongoService.INSTANCE.getUsersCollection().find(Filters.eq("_id", id)).first();
if (document != null) {
BatUser user = new BatUser(id, document);
users.put(id, user);
log.info("Loaded user \"{}\" in {}ms", user.getName(),System.currentTimeMillis() - before);
return user;
}
BatUser user = userRepository.save(new BatUser(id));
log.info("Created user for \"{}\" in {}ms", user.getDiscordUser().getName(), System.currentTimeMillis() - start);
// New user
BatUser user = new BatUser(id, new Document());
users.put(id, user);
log.info("Created user \"{}\" - \"{}\"", user.getName(), user.getId());
return user;
}
/**
* Saves a user
*
* @param user The user to save
*/
public void saveUser(@NonNull BatUser user) {
userRepository.save(user);
}
}