diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index ac77e03..ebb497d 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -208,7 +208,8 @@ declare -g LAST_CRON_TIME="" # Instead of running find 23 times, run once and reuse results declare -g WP_SITES_CACHE="" declare -g WP_CACHE_INITIALIZED=0 -declare -g WP_CACHE_FILE="/tmp/wp-sites-cache-$$" +declare -g WP_CACHE_FILE="/tmp/wp-sites-cache" # Persistent across invocations (no $$) +declare -g WP_CACHE_TTL=3600 # Cache valid for 1 hour (3600 seconds) # OPTIMIZATION: Function Registry (OPT-14) # Maintains a registry of all available functions for discoverability and validation @@ -325,11 +326,16 @@ get_home_path() { initialize_wp_cache() { local panel="$SYS_CONTROL_PANEL" - # Check if cache file already exists from earlier in this session + # Check if cache file exists and is still fresh (within TTL) if [ -f "$WP_CACHE_FILE" ]; then - WP_SITES_CACHE=$(cat "$WP_CACHE_FILE") - WP_CACHE_INITIALIZED=1 - return + local cache_age=$(($(date +%s) - $(stat -c %Y "$WP_CACHE_FILE" 2>/dev/null || echo 0))) + if [ "$cache_age" -lt "$WP_CACHE_TTL" ]; then + # Cache is fresh, use it + WP_SITES_CACHE=$(cat "$WP_CACHE_FILE") + WP_CACHE_INITIALIZED=1 + return + fi + # Cache is stale, will refresh below fi # Run the discovery and save to temp file for persistence @@ -342,21 +348,25 @@ initialize_wp_cache() { # Returns cached results from initialize_wp_cache() # Always returns instantly (uses file cache) get_wp_sites_cached() { - # Quick check: if we have the temp file, use it immediately (no discovery needed) + # Quick check: if we have a fresh cache file, use it immediately (no discovery needed) if [ -f "$WP_CACHE_FILE" ]; then - cat "$WP_CACHE_FILE" - return + local cache_age=$(($(date +%s) - $(stat -c %Y "$WP_CACHE_FILE" 2>/dev/null || echo 0))) + if [ "$cache_age" -lt "$WP_CACHE_TTL" ]; then + # Cache is fresh, return it + cat "$WP_CACHE_FILE" + return + fi fi - # Fallback: Initialize cache if not already done + # Cache missing or stale: Initialize/refresh cache if [ "$WP_CACHE_INITIALIZED" = "0" ]; then initialize_wp_cache fi echo "$WP_SITES_CACHE" } -# Cleanup cache on exit -trap 'rm -f "$WP_CACHE_FILE" "$LOCK_FILE"; rollback_cleanup' EXIT INT TERM +# Cleanup on exit (keep cache file for next invocation, only remove lock file) +trap 'rm -f "$LOCK_FILE"; rollback_cleanup' EXIT INT TERM # OPTIMIZATION: User extraction caching (memoization) # extract_user_from_path() called 10 times, often for same path @@ -1603,12 +1613,25 @@ enable_wpcron_in_config() { clear print_banner "WordPress Cron Manager" -# PERFORMANCE: Pre-load WordPress sites cache on startup (done once per menu cycle) -# This eliminates the long initial scan and makes all operations fast +# PERFORMANCE: Pre-load WordPress sites cache on startup +# Check if we have a fresh cached copy from a previous run (usually instant) if [ "$WP_CACHE_INITIALIZED" = "0" ]; then - echo -e "${CYAN}Scanning for WordPress installations...${NC}" - initialize_wp_cache - echo -e "${GREEN}✓ Cache loaded${NC}" + if [ -f "$WP_CACHE_FILE" ]; then + local cache_age=$(($(date +%s) - $(stat -c %Y "$WP_CACHE_FILE" 2>/dev/null || echo 0))) + if [ "$cache_age" -lt "$WP_CACHE_TTL" ]; then + echo -e "${GREEN}✓ Using cached WordPress site list (refreshed hourly)${NC}" + WP_SITES_CACHE=$(cat "$WP_CACHE_FILE") + WP_CACHE_INITIALIZED=1 + else + echo -e "${CYAN}Refreshing WordPress site list (cache expired)...${NC}" + initialize_wp_cache + echo -e "${GREEN}✓ Cache refreshed${NC}" + fi + else + echo -e "${CYAN}Scanning for WordPress installations (first run)...${NC}" + initialize_wp_cache + echo -e "${GREEN}✓ Cache loaded${NC}" + fi echo "" fi