|
| 1 | +package de.php_perfect.intellij.ddev.addon; |
| 2 | + |
| 3 | +import com.intellij.openapi.application.ApplicationManager; |
| 4 | +import com.intellij.openapi.diagnostic.Logger; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.openapi.project.ProjectManager; |
| 7 | +import com.intellij.openapi.project.ProjectManagerListener; |
| 8 | +import de.php_perfect.intellij.ddev.cmd.DdevAddon; |
| 9 | +import org.jetbrains.annotations.NotNull; |
| 10 | +import org.jetbrains.annotations.Nullable; |
| 11 | + |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 17 | +import java.util.concurrent.atomic.AtomicLong; |
| 18 | + |
| 19 | +/** |
| 20 | + * Cache for DDEV addons to avoid repeated calls to the DDEV CLI. |
| 21 | + */ |
| 22 | +public class AddonCache { |
| 23 | + private static final Logger LOG = Logger.getInstance(AddonCache.class); |
| 24 | + private static final ConcurrentHashMap<Project, AddonCache> INSTANCES = new ConcurrentHashMap<>(); |
| 25 | + // Cache expiration time: 24 hours in milliseconds |
| 26 | + private static final long CACHE_EXPIRATION_TIME = 24L * 60 * 60 * 1000; |
| 27 | + |
| 28 | + private final Project project; |
| 29 | + private final AtomicBoolean isRefreshing = new AtomicBoolean(false); |
| 30 | + private List<DdevAddon> availableAddons = Collections.synchronizedList(Collections.emptyList()); |
| 31 | + private final AtomicLong lastRefreshTime = new AtomicLong(0); |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets the AddonCache instance for the given project. |
| 35 | + * |
| 36 | + * @param project The project to get the cache for |
| 37 | + * @return The AddonCache instance |
| 38 | + */ |
| 39 | + public static @NotNull AddonCache getInstance(@NotNull Project project) { |
| 40 | + return INSTANCES.computeIfAbsent(project, AddonCache::new); |
| 41 | + } |
| 42 | + |
| 43 | + private AddonCache(@NotNull Project project) { |
| 44 | + this.project = project; |
| 45 | + |
| 46 | + // Initialize the cache in the background |
| 47 | + refreshCacheAsync(); |
| 48 | + |
| 49 | + // Register a project dispose listener to remove the cache when the project is closed |
| 50 | + ProjectManager.getInstance().addProjectManagerListener(project, new ProjectManagerListener() { |
| 51 | + @Override |
| 52 | + public void projectClosed(@NotNull Project closedProject) { |
| 53 | + if (project.equals(closedProject)) { |
| 54 | + INSTANCES.remove(project); |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Gets the list of available addons from the cache. |
| 62 | + * If the cache is empty or expired, it will be refreshed asynchronously. |
| 63 | + * |
| 64 | + * @return The list of available addons |
| 65 | + */ |
| 66 | + public @NotNull List<DdevAddon> getAvailableAddons() { |
| 67 | + // If the cache is empty or expired, refresh it asynchronously |
| 68 | + if (availableAddons.isEmpty() || isCacheExpired()) { |
| 69 | + refreshCacheAsync(); |
| 70 | + } |
| 71 | + return availableAddons; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Checks if the cache is expired. |
| 76 | + * |
| 77 | + * @return true if the cache is expired, false otherwise |
| 78 | + */ |
| 79 | + private boolean isCacheExpired() { |
| 80 | + return System.currentTimeMillis() - lastRefreshTime.get() > CACHE_EXPIRATION_TIME; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Refreshes the cache asynchronously. |
| 85 | + */ |
| 86 | + public void refreshCacheAsync() { |
| 87 | + // If already refreshing, don't start another refresh |
| 88 | + if (isRefreshing.compareAndSet(false, true)) { |
| 89 | + LOG.debug("Starting async refresh of addon cache for project: " + project.getName()); |
| 90 | + |
| 91 | + ApplicationManager.getApplication().executeOnPooledThread(() -> { |
| 92 | + try { |
| 93 | + List<DdevAddon> addons = fetchAvailableAddons(); |
| 94 | + if (addons != null) { |
| 95 | + availableAddons = addons; |
| 96 | + lastRefreshTime.set(System.currentTimeMillis()); |
| 97 | + LOG.info("Refreshed addon cache for project: " + project.getName() + ", found " + addons.size() + " addons"); |
| 98 | + } |
| 99 | + } catch (Exception e) { |
| 100 | + LOG.error("Failed to refresh addon cache for project: " + project.getName(), e); |
| 101 | + } finally { |
| 102 | + isRefreshing.set(false); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Fetches the list of available addons from the DDEV CLI. |
| 110 | + * |
| 111 | + * @return The list of available addons, or null if the operation failed |
| 112 | + */ |
| 113 | + @Nullable |
| 114 | + private List<DdevAddon> fetchAvailableAddons() { |
| 115 | + // Execute the DDEV command to get all available add-ons |
| 116 | + Map<String, Object> jsonObject = AddonUtils.executeAddonCommand(project, "list", "--all", "--json-output"); |
| 117 | + |
| 118 | + // Parse the JSON response into a list of DdevAddon objects |
| 119 | + List<DdevAddon> addons = AddonUtils.parseAvailableAddons(jsonObject); |
| 120 | + |
| 121 | + // Return null if no addons were found (to trigger appropriate logging) |
| 122 | + return addons.isEmpty() ? null : addons; |
| 123 | + } |
| 124 | +} |
0 commit comments