forked from Fascinated/Bat
add guild, user and scoresaber account caching
This commit is contained in:
@ -2,22 +2,28 @@ package cc.fascinated.bat.service;
|
||||
|
||||
import cc.fascinated.bat.model.BatUser;
|
||||
import cc.fascinated.bat.repository.UserRepository;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Service @Log4j2
|
||||
@Service @Log4j2 @Getter
|
||||
@DependsOn("discordService")
|
||||
public class UserService {
|
||||
/**
|
||||
* The cached users
|
||||
*/
|
||||
private final Map<String, BatUser> users = new HashMap<>();
|
||||
|
||||
/**
|
||||
* The user repository to use
|
||||
*/
|
||||
@ -34,12 +40,17 @@ public class UserService {
|
||||
* @param id The ID of the user
|
||||
* @return The user
|
||||
*/
|
||||
@Cacheable(cacheNames = "users", key = "#id")
|
||||
public BatUser getUser(@NonNull String id) {
|
||||
if (users.containsKey(id)) {
|
||||
return users.get(id);
|
||||
}
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
Optional<BatUser> optionalUser = userRepository.findById(id);
|
||||
if (optionalUser.isPresent()) {
|
||||
return optionalUser.get();
|
||||
BatUser user = optionalUser.get();
|
||||
users.put(id, user);
|
||||
return user;
|
||||
}
|
||||
BatUser user = userRepository.save(new BatUser(id));
|
||||
log.info("Created user \"{}\" in {}ms", id, System.currentTimeMillis() - start);
|
||||
@ -51,7 +62,6 @@ public class UserService {
|
||||
*
|
||||
* @param user The user to save
|
||||
*/
|
||||
@CachePut(cacheNames = "users", key = "#user.id")
|
||||
public void saveUser(@NonNull BatUser user) {
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
Reference in New Issue
Block a user