-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abc6b72
commit e4ae31a
Showing
13 changed files
with
172 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
common/src/main/java/net/lindseybot/shared/services/CacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package net.lindseybot.shared.services; | ||
|
||
import lombok.Getter; | ||
import net.jodah.expiringmap.ExpirationPolicy; | ||
import net.jodah.expiringmap.ExpiringMap; | ||
import net.lindseybot.shared.entities.profile.servers.AntiAd; | ||
import net.lindseybot.shared.entities.profile.servers.AntiScam; | ||
import net.lindseybot.shared.entities.profile.servers.Registration; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Getter | ||
public class CacheService { | ||
|
||
private final Map<Long, Boolean> roleHistory = ExpiringMap.builder() | ||
.expirationPolicy(ExpirationPolicy.ACCESSED) | ||
.expiration(10, TimeUnit.MINUTES) | ||
.maxSize(50_000) | ||
.build(); | ||
|
||
private final Map<Long, AntiScam> antiScam = ExpiringMap.builder() | ||
.expirationPolicy(ExpirationPolicy.ACCESSED) | ||
.expiration(10, TimeUnit.MINUTES) | ||
.maxSize(50_000) | ||
.build(); | ||
|
||
private final Map<Long, AntiAd> antiAd = ExpiringMap.builder() | ||
.expirationPolicy(ExpirationPolicy.ACCESSED) | ||
.expiration(30, TimeUnit.MINUTES) | ||
.maxSize(50_000) | ||
.build(); | ||
|
||
private final Map<Long, Registration> registration = ExpiringMap.builder() | ||
.expirationPolicy(ExpirationPolicy.ACCESSED) | ||
.expiration(30, TimeUnit.MINUTES) | ||
.maxSize(50_000) | ||
.build(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 10 additions & 17 deletions
27
module-automod/src/main/java/net/lindseybot/automod/services/RegistrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,36 @@ | ||
package net.lindseybot.automod.services; | ||
|
||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.jodah.expiringmap.ExpirationPolicy; | ||
import net.jodah.expiringmap.ExpiringMap; | ||
import net.lindseybot.automod.repositories.sql.RegistrationRepository; | ||
import net.lindseybot.shared.entities.profile.servers.Registration; | ||
import net.lindseybot.shared.services.CacheService; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Service | ||
public class RegistrationService { | ||
|
||
private final RegistrationRepository repository; | ||
private final ExpiringMap<Long, Registration> cache = ExpiringMap.builder() | ||
.expirationPolicy(ExpirationPolicy.ACCESSED) | ||
.expiration(1, TimeUnit.MINUTES) | ||
.maxSize(10_000) | ||
.build(); | ||
private final CacheService cache; | ||
|
||
public RegistrationService(RegistrationRepository repository) { | ||
public RegistrationService(RegistrationRepository repository, CacheService cache) { | ||
this.repository = repository; | ||
this.cache = cache; | ||
} | ||
|
||
public Registration find(Guild guild) { | ||
Registration cached = this.cache.get(guild.getIdLong()); | ||
if (cached != null) { | ||
return cached; | ||
if (this.cache.getRegistration().containsKey(guild.getIdLong())) { | ||
return this.cache.getRegistration().get(guild.getIdLong()); | ||
} | ||
Registration registration = repository.findById(guild.getIdLong()) | ||
var data = repository.findById(guild.getIdLong()) | ||
.orElse(new Registration(guild.getIdLong())); | ||
this.cache.put(guild.getIdLong(), registration); | ||
return registration; | ||
this.cache.getRegistration().put(guild.getIdLong(), data); | ||
return data; | ||
} | ||
|
||
public void disable(Registration registration) { | ||
registration.setEnabled(false); | ||
this.repository.save(registration); | ||
this.cache.remove(registration.getGuild()); | ||
this.cache.getRegistration().remove(registration.getGuild()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.