Skip to content

Commit

Permalink
fix: L2 cache should expire after two hours.
Browse files Browse the repository at this point in the history
  • Loading branch information
QwQ-dev committed Jan 3, 2025
1 parent 5d9a8be commit 17645a7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@InjectableComponent
public class PlayerLauncher extends Plugin {
@Autowired
@SuppressWarnings("unused")
private AnnotationProcessingServiceInterface annotationProcessingService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Service for managing {@link LegacyPlayerData} with a multi-level caching system.
*
* @author qwq-dev
* @since 2025-01-03
* @since 2025-01-03 15:12
*/
@Getter
public class LegacyPlayerDataService {
Expand All @@ -32,27 +32,14 @@ public class LegacyPlayerDataService {
*/
public static final CacheServiceInterface<Cache<String, LegacyPlayerDataService>, LegacyPlayerDataService>
LEGACY_PLAYER_DATA_SERVICES = CacheServiceFactory.createCaffeineCache();

/**
* Retrieves a {@link LegacyPlayerDataService} by name.
*
* @param name the name of the service
* @return an {@link Optional} containing the {@link LegacyPlayerDataService} if found, or empty if not found
*/
public static Optional<LegacyPlayerDataService> getLegacyPlayerDataService(String name) {
return Optional.ofNullable(LEGACY_PLAYER_DATA_SERVICES.getCache().getIfPresent(name));
}

/**
* The name of the service.
*/
private final String name;

/**
* MongoDB connection configuration for database operations.
*/
private final MongoDBConnectionConfig mongoDBConnectionConfig;

/**
* Flexible multi-level cache service managing L1 and L2 caches.
*/
Expand Down Expand Up @@ -93,6 +80,16 @@ public LegacyPlayerDataService(String name, MongoDBConnectionConfig mongoDBConne
cache.put(name, this);
}

/**
* Retrieves a {@link LegacyPlayerDataService} by name.
*
* @param name the name of the service
* @return an {@link Optional} containing the {@link LegacyPlayerDataService} if found, or empty if not found
*/
public static Optional<LegacyPlayerDataService> getLegacyPlayerDataService(String name) {
return Optional.ofNullable(LEGACY_PLAYER_DATA_SERVICES.getCache().getIfPresent(name));
}

/**
* Retrieves the {@link LegacyPlayerData} from the first-level cache (L1).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import net.legacy.library.player.util.KeyUtil;
import org.redisson.api.RedissonClient;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.function.Function;
Expand Down Expand Up @@ -60,8 +62,8 @@ public ScheduledTask<?> start() {

l2Cache.execute(
lockFunction,
cache -> {
cache.getBucket(bucketKey).set(serialized);
client -> {
client.getBucket(bucketKey).set(serialized, Duration.of(2, ChronoUnit.HOURS));
return null;
},
lockSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class KeyUtil {
/**
* Generates a key for a {@link LegacyPlayerDataService} and a {@link UUID}.
*
* @param uuid the UUID
* @param uuid the UUID
* @param legacyPlayerDataService the {@link LegacyPlayerDataService}
* @param strings additional strings to append to the key
* @param strings additional strings to append to the key
* @return the key
*/
public static String getLegacyPlayerDataServiceKey(UUID uuid, LegacyPlayerDataService legacyPlayerDataService, String... strings) {
Expand Down

0 comments on commit 17645a7

Please sign in to comment.