1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-15 01:45:06 +00:00

Apply scan cache to simplify

This commit is contained in:
Tw93
2025-11-15 13:20:58 +08:00
parent a4561ab516
commit cccc41990e
4 changed files with 520 additions and 546 deletions

View File

@@ -100,42 +100,18 @@ format_last_used_summary() {
# Scan applications and collect information # Scan applications and collect information
scan_applications() { scan_applications() {
# Cache configuration # Simplified cache: only check timestamp (24h TTL)
local cache_dir="$HOME/.cache/mole" local cache_dir="$HOME/.cache/mole"
local cache_file="$cache_dir/app_scan_cache" local cache_file="$cache_dir/app_scan_cache"
local cache_meta="$cache_dir/app_scan_meta" local cache_ttl=86400 # 24 hours
local cache_dir_mtime="$cache_dir/app_dir_mtime"
local cache_ttl=86400 # 24 hours cache validity (app count change will trigger refresh)
mkdir -p "$cache_dir" 2> /dev/null mkdir -p "$cache_dir" 2> /dev/null
# Get modification time of app directories to detect new installations # Check if cache exists and is fresh
local sys_app_mtime if [[ -f "$cache_file" ]]; then
sys_app_mtime=$(stat -f%m "/Applications" 2> /dev/null || echo "0") local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 86401)))
local user_app_mtime if [[ $cache_age -lt $cache_ttl ]]; then
user_app_mtime=$(stat -f%m "$HOME/Applications" 2> /dev/null || echo "0") # Cache hit - return immediately
local combined_mtime=$((sys_app_mtime + user_app_mtime))
# Quick count of current apps (system + user directories)
local current_app_count
current_app_count=$(
(
find /Applications -name "*.app" -maxdepth 1 2> /dev/null
find ~/Applications -name "*.app" -maxdepth 1 2> /dev/null
) | wc -l | tr -d ' '
)
# Check if cache is valid unless explicitly disabled
if [[ -f "$cache_file" && -f "$cache_meta" && -f "$cache_dir_mtime" ]]; then
local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 0)))
local cached_app_count
cached_app_count=$(cat "$cache_meta" 2> /dev/null || echo "0")
local cached_dir_mtime
cached_dir_mtime=$(cat "$cache_dir_mtime" 2> /dev/null || echo "0")
# Cache is valid if: age < TTL AND app count matches AND directory not modified
if [[ $cache_age -lt $cache_ttl && "$cached_app_count" == "$current_app_count" && "$cached_dir_mtime" == "$combined_mtime" ]]; then
# Silent - cache hit, return immediately without any output
echo "$cache_file" echo "$cache_file"
return 0 return 0
fi fi
@@ -381,12 +357,10 @@ scan_applications() {
} }
rm -f "$temp_file" rm -f "$temp_file"
# Update cache with app count metadata and directory modification time # Save to cache (simplified - no metadata)
cp "${temp_file}.sorted" "$cache_file" 2> /dev/null || true cp "${temp_file}.sorted" "$cache_file" 2> /dev/null || true
echo "$current_app_count" > "$cache_meta" 2> /dev/null || true
echo "$combined_mtime" > "$cache_dir_mtime" 2> /dev/null || true
# Verify sorted file exists before returning # Return sorted file
if [[ -f "${temp_file}.sorted" ]]; then if [[ -f "${temp_file}.sorted" ]]; then
echo "${temp_file}.sorted" echo "${temp_file}.sorted"
else else
@@ -613,20 +587,13 @@ main() {
# Hide cursor during operation # Hide cursor during operation
hide_cursor hide_cursor
# Quick cache validity check first (minimal I/O) # Simplified: always check if we need alt screen for scanning
local cache_dir="$HOME/.cache/mole" # (scan_applications handles cache internally)
local cache_file="$cache_dir/app_scan_cache"
local cache_meta="$cache_dir/app_scan_meta"
local cache_ttl=86400
local needs_scanning=true local needs_scanning=true
local cache_file="$HOME/.cache/mole/app_scan_cache"
# Fast preliminary check: cache exists and not expired if [[ -f "$cache_file" ]]; then
if [[ -f "$cache_file" && -f "$cache_meta" ]]; then
local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 86401))) local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 86401)))
if [[ $cache_age -lt $cache_ttl ]]; then [[ $cache_age -lt 86400 ]] && needs_scanning=false
# Cache age is OK, now check app count (delegate to scan_applications)
needs_scanning=false
fi
fi fi
# Only enter alt screen if we need scanning (shows progress) # Only enter alt screen if we need scanning (shows progress)

View File

@@ -237,13 +237,20 @@ EOF
# Conditional items # Conditional items
local item local item
item=$(check_startup_items || true); [[ -n "$item" ]] && items+=("$item") item=$(check_startup_items || true)
item=$(check_cache_refresh || true); [[ -n "$item" ]] && items+=("$item") [[ -n "$item" ]] && items+=("$item")
item=$(check_mail_downloads || true); [[ -n "$item" ]] && items+=("$item") item=$(check_cache_refresh || true)
item=$(check_saved_state || true); [[ -n "$item" ]] && items+=("$item") [[ -n "$item" ]] && items+=("$item")
item=$(check_swap_cleanup || true); [[ -n "$item" ]] && items+=("$item") item=$(check_mail_downloads || true)
item=$(check_local_snapshots || true); [[ -n "$item" ]] && items+=("$item") [[ -n "$item" ]] && items+=("$item")
item=$(check_developer_cleanup || true); [[ -n "$item" ]] && items+=("$item") item=$(check_saved_state || true)
[[ -n "$item" ]] && items+=("$item")
item=$(check_swap_cleanup || true)
[[ -n "$item" ]] && items+=("$item")
item=$(check_local_snapshots || true)
[[ -n "$item" ]] && items+=("$item")
item=$(check_developer_cleanup || true)
[[ -n "$item" ]] && items+=("$item")
# Output items as JSON # Output items as JSON
local first=true local first=true

View File

@@ -67,13 +67,13 @@ echo -e "${YELLOW}4. Checking code optimizations...${NC}"
OPTIMIZATION_SCORE=0 OPTIMIZATION_SCORE=0
TOTAL_CHECKS=0 TOTAL_CHECKS=0
# Check 1: Simplified keyboard input (0.05s timeout) # Check 1: Keyboard input handling (restored to 1s for reliability)
((TOTAL_CHECKS++)) ((TOTAL_CHECKS++))
if grep -q "read -r -s -n 1 -t 0.05" lib/common.sh; then if grep -q "read -r -s -n 1 -t 1" lib/common.sh; then
echo -e "${GREEN} ✓ Keyboard timeout optimized (0.05s)${NC}" echo -e "${GREEN} ✓ Keyboard timeout properly configured (1s)${NC}"
((OPTIMIZATION_SCORE++)) ((OPTIMIZATION_SCORE++))
else else
echo -e "${YELLOW} ⚠ Keyboard timeout not optimized${NC}" echo -e "${YELLOW} ⚠ Keyboard timeout may be misconfigured${NC}"
fi fi
# Check 2: Single-pass drain_pending_input # Check 2: Single-pass drain_pending_input