Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ public void onRuneScapeProfileChanged(RuneScapeProfileChanged event)
{
log.info("\nReceived RuneScape profile change event from '{}' to '{}'", oldProfile, newProfile);
if (microbotConfig.isRs2CacheEnabled()) {
// Use async profile change to avoid blocking client thread
Rs2CacheManager.handleProfileChange(newProfile, oldProfile);
log.info("Initiated async profile change from '{}' to '{}'", oldProfile, newProfile);
}
return;
}
Expand Down Expand Up @@ -435,7 +437,8 @@ public void onConfigChanged(ConfigChanged ev)
}
break;
case MicrobotConfig.keyEnableCache:
Microbot.showMessage("Restart your client to apply cache changes");
// Handle dynamic cache system initialization/shutdown
handleCacheConfigChange(ev.getNewValue());
break;
default:
break;
Expand Down Expand Up @@ -535,9 +538,17 @@ public void onGameTick(GameTick event)
@Subscribe(priority = 100)
private void onClientShutdown(ClientShutdown e)
{
// Save all caches through Rs2CacheManager
// Save all caches through Rs2CacheManager using async operations
if (microbotConfig.isRs2CacheEnabled()) {
Rs2CacheManager.savePersistentCaches();
try {
// Use async save but wait for completion during shutdown
Rs2CacheManager.savePersistentCachesAsync().get(30, java.util.concurrent.TimeUnit.SECONDS);
log.info("Successfully saved all caches asynchronously during shutdown");
} catch (Exception ex) {
log.error("Failed to save caches during shutdown: {}", ex.getMessage(), ex);
// Fallback to synchronous save if async fails
Rs2CacheManager.savePersistentCaches();
}
Rs2CacheManager.getInstance().close();
}
}
Expand All @@ -548,12 +559,20 @@ private void onClientShutdown(ClientShutdown e)
*/
private void initializeCacheSystem() {
try {
// Check if already initialized
if (Rs2CacheManager.isEventHandlersRegistered()) {
log.debug("Cache system already initialized, skipping");
return;
}

// Get the cache manager instance
Rs2CacheManager cacheManager = Rs2CacheManager.getInstance();

// Set the EventBus for cache event handling (without loading caches yet)
Rs2CacheManager.setEventBus(eventBus);

// Register event handlers
Rs2CacheManager.registerEventHandlers();

// Keep deprecated EntityCache for backward compatibility (for now)
//Rs2EntityCache.getInstance();
Expand All @@ -572,10 +591,19 @@ private void initializeCacheSystem() {
*/
private void shutdownCacheSystem() {
try {
// Check if already shutdown
if (!Rs2CacheManager.isEventHandlersRegistered()) {
log.debug("Cache system already shutdown, skipping");
return;
}

Rs2CacheManager cacheManager = Rs2CacheManager.getInstance();

log.debug("Final cache statistics before shutdown: {}", cacheManager.getCacheStatistics());

// Unregister event handlers first
Rs2CacheManager.unregisterEventHandlers();

// Close the cache manager and all caches
cacheManager.close();

Expand All @@ -599,6 +627,26 @@ private void shutdownCacheSystem() {
log.error("Error during cache system shutdown: {}", e.getMessage(), e);
}
}

/**
* Handles cache configuration changes dynamically without requiring client restart.
* This method is called when the user changes the "Enable Microbot Cache" config option.
*
* @param newValue The new value of the cache enable config ("true" or "false")
*/
private void handleCacheConfigChange(String newValue) {
boolean enableCache = Objects.equals(newValue, "true");

if (enableCache) {
log.info("Cache system enabled via config change - initializing...");
initializeCacheSystem();
Microbot.showMessage("Cache system enabled successfully");
} else {
log.info("Cache system disabled via config change - shutting down...");
shutdownCacheSystem();
Microbot.showMessage("Cache system disabled successfully");
}
}
/**
* Dynamically checks if any visible widget overlaps with the specified bounds
* @param overlayBoundsCanvas The bounds to check for widget overlap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import net.runelite.client.plugins.microbot.util.poh.PohTeleports;
import net.runelite.client.plugins.microbot.util.tabs.Rs2Tab;
import net.runelite.client.plugins.microbot.util.walker.Rs2Walker;

import net.runelite.client.plugins.microbot.util.cache.Rs2SkillCache;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -542,8 +542,14 @@ private boolean hasRequiredLevels(Transport transport) {
int[] requiredLevels = transport.getSkillLevels();
Skill[] skills = Skill.values();
return IntStream.range(0, requiredLevels.length)
.filter(i -> requiredLevels[i] > 0)
.allMatch(i -> Microbot.getClient().getBoostedSkillLevel(skills[i]) >= requiredLevels[i]);
.filter(i -> requiredLevels[i] > 0)
.allMatch(i -> {
if (Microbot.isRs2CacheEnabled()) {
return Rs2SkillCache.getBoostedSkillLevel(skills[i]) >= requiredLevels[i];
} else {
return Microbot.getClient().getBoostedSkillLevel(skills[i]) >= requiredLevels[i];
}
});
}

/**
Expand All @@ -553,8 +559,14 @@ private boolean hasRequiredLevels(Restriction restriction) {
int[] requiredLevels = restriction.getSkillLevels();
Skill[] skills = Skill.values();
return IntStream.range(0, requiredLevels.length)
.filter(i -> requiredLevels[i] > 0)
.allMatch(i -> Microbot.getClient().getBoostedSkillLevel(skills[i]) >= requiredLevels[i]);
.filter(i -> requiredLevels[i] > 0)
.allMatch(i -> {
if (Microbot.isRs2CacheEnabled()) {
return Rs2SkillCache.getBoostedSkillLevel(skills[i]) >= requiredLevels[i];
} else {
return Microbot.getClient().getBoostedSkillLevel(skills[i]) >= requiredLevels[i];
}
});
}

private void updateActionBasedOnQuestState(Transport transport) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,11 @@ public synchronized void invalidateAll() {
* Gets the cache timestamp for a specific key.
*
* @param key The key to get the timestamp for
* @return The timestamp when the key was cached, or null if not found
* @return The timestamp when the key was cached, or -1 if not found
*/
public Long getCacheTimestamp(K key) {
return cacheTimestamps.get(key);
public long getCacheTimestamp(K key) {
Long timestamp = cacheTimestamps.get(key);
return timestamp != null ? timestamp : 0L;
}

// ============================================
Expand Down
Loading