rust-mc-status 2.0.0

High-performance asynchronous Rust library for querying Minecraft server status (Java & Bedrock)
Documentation
//! Cache management example for the rust-mc-status library.
//!
//! This example demonstrates how to work with DNS and SRV caches:
//! - Viewing cache statistics
//! - Clearing specific caches
//! - Understanding cache behavior
//! - Performance impact of caching

use rust_mc_status::{McClient, ServerEdition};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Cache Management Example ===\n");

    let client = McClient::new()
        .with_timeout(Duration::from_secs(5))
        .with_max_parallel(10);

    // Initial cache state
    println!("1. Initial Cache State:");
    let stats = client.cache_stats();
    println!("   DNS cache entries: {}", stats.dns_entries);
    println!("   SRV cache entries: {}", stats.srv_entries);
    println!();

    // Ping some servers to populate cache
    println!("2. Pinging servers to populate cache...");
    let _ = client.ping("mc.hypixel.net", ServerEdition::Java).await;
    let _ = client.ping("geo.hivebedrock.network:19132", ServerEdition::Bedrock).await;
    
    let stats = client.cache_stats();
    println!("   After pinging 2 servers:");
    println!("   DNS cache entries: {}", stats.dns_entries);
    println!("   SRV cache entries: {}", stats.srv_entries);
    println!();

    // Ping same servers again (should use cache)
    println!("3. Pinging same servers again (cache should be used):");
    let start = std::time::Instant::now();
    let _ = client.ping("mc.hypixel.net", ServerEdition::Java).await;
    let cached_time = start.elapsed();
    println!("   Time with cache: {:.2} ms", cached_time.as_secs_f64() * 1000.0);
    println!();

    // Clear DNS cache and ping again
    println!("4. Clearing DNS cache and pinging again:");
    client.clear_dns_cache();
    let stats = client.cache_stats();
    println!("   DNS cache entries: {}", stats.dns_entries);
    println!("   SRV cache entries: {} (still cached)", stats.srv_entries);
    
    let start = std::time::Instant::now();
    let _ = client.ping("mc.hypixel.net", ServerEdition::Java).await;
    let after_clear_time = start.elapsed();
    println!("   Time after clearing DNS cache: {:.2} ms", after_clear_time.as_secs_f64() * 1000.0);
    println!();

    // Clear all caches
    println!("5. Clearing all caches:");
    client.clear_all_caches();
    let stats = client.cache_stats();
    println!("   DNS cache entries: {}", stats.dns_entries);
    println!("   SRV cache entries: {}", stats.srv_entries);
    println!();

    // Demonstrate cache effectiveness - measure DNS resolution time separately
    println!("6. Cache Effectiveness Demonstration (DNS Resolution):");
    println!("   Measuring DNS resolution time (not full ping)...");
    
    // Clear cache first to ensure cold start
    client.clear_all_caches();
    
    // First resolution (cold cache - actual DNS lookup)
    let (addr1, time1) = client.resolve_dns_timed("mc.hypixel.net", 25565).await
        .map_err(|e| format!("DNS resolution failed: {}", e))?;
    println!("   First DNS resolution (cold cache): {:.2} ms", time1);
    println!("   Resolved to: {}", addr1);
    
    // Second resolution (warm cache - from cache)
    let (addr2, time2) = client.resolve_dns_timed("mc.hypixel.net", 25565).await
        .map_err(|e| format!("DNS resolution failed: {}", e))?;
    println!("   Second DNS resolution (warm cache): {:.2} ms", time2);
    println!("   Resolved to: {}", addr2);
    
    if time1 > 0.0 && time2 > 0.0 {
        let speedup = time1 / time2;
        let improvement = ((time1 - time2) / time1 * 100.0).max(0.0);
        println!();
        println!("   Cache speedup: {:.1}x faster", speedup);
        println!("   Cache improvement: {:.1}% faster", improvement);
        println!("   Time saved: {:.2} ms per resolution", time1 - time2);
    }
    
    println!();
    println!("   Note: Full ping time includes network latency (200-500ms),");
    println!("   so DNS cache (10-50ms) has less visible impact on total time.");
    println!("   However, DNS cache is very effective for repeated queries!");

    println!();
    println!("{}", "=".repeat(60));
    println!();
    println!("Cache Management Tips:");
    println!("  - DNS and SRV caches have TTL of 5 minutes");
    println!("  - Cache improves performance for repeated queries");
    println!("  - Use clear_all_caches() to free memory");
    println!("  - Use clear_dns_cache() or clear_srv_cache() for selective clearing");
    println!("  - Use cache_stats() to monitor cache usage");

    Ok(())
}