|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + httpcache "github.com/sandrolain/httpcache" |
| 11 | + "github.com/sandrolain/httpcache/diskcache" |
| 12 | + rediscache "github.com/sandrolain/httpcache/redis" |
| 13 | + "github.com/sandrolain/httpcache/wrapper/multicache" |
| 14 | + |
| 15 | + "github.com/gomodule/redigo/redis" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + jsonURL = "https://httpbin.org/json" |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + fmt.Println("MultiCache Example - Three-Tier Caching Strategy") |
| 24 | + fmt.Println("=================================================") |
| 25 | + fmt.Println("") |
| 26 | + |
| 27 | + // Tier 1: In-memory cache (fast, small, volatile) |
| 28 | + // 10 MB limit, evicts least recently used |
| 29 | + memoryCache := httpcache.NewMemoryCache() |
| 30 | + fmt.Println("✓ Tier 1: Memory cache initialized (fast, volatile)") |
| 31 | + |
| 32 | + // Tier 2: Disk cache (medium speed, larger, persistent) |
| 33 | + // 100 MB limit, survives restarts |
| 34 | + diskCache := diskcache.New("/tmp/httpcache-multicache-example") |
| 35 | + fmt.Println("✓ Tier 2: Disk cache initialized (medium speed, persistent)") |
| 36 | + |
| 37 | + // Tier 3: Redis cache (network-based, largest, shared) |
| 38 | + // Optional - only if Redis is available |
| 39 | + var mc *multicache.MultiCache |
| 40 | + redisConn, err := redis.Dial("tcp", "localhost:6379") |
| 41 | + if err != nil { |
| 42 | + fmt.Println("⚠ Tier 3: Redis not available, using only 2 tiers") |
| 43 | + mc = multicache.New(memoryCache, diskCache) |
| 44 | + } else { |
| 45 | + fmt.Println("✓ Tier 3: Redis cache initialized (network-based, shared)") |
| 46 | + redisCache := rediscache.NewWithClient(redisConn) |
| 47 | + mc = multicache.New(memoryCache, diskCache, redisCache) |
| 48 | + } |
| 49 | + |
| 50 | + // Create HTTP client with multi-tier caching |
| 51 | + transport := httpcache.NewTransport(mc) |
| 52 | + client := &http.Client{ |
| 53 | + Transport: transport, |
| 54 | + Timeout: 10 * time.Second, |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Println("\nSetup complete!") |
| 58 | + fmt.Println("") |
| 59 | + |
| 60 | + // Example 1: Initial request (cache miss, writes to all tiers) |
| 61 | + fmt.Println("Example 1: Initial request") |
| 62 | + fmt.Println("---------------------------") |
| 63 | + makeRequest(client, jsonURL) |
| 64 | + fmt.Println() |
| 65 | + |
| 66 | + // Example 2: Subsequent request (cache hit from tier 1 - fastest) |
| 67 | + fmt.Println("Example 2: Second request (should hit tier 1 - memory)") |
| 68 | + fmt.Println("-------------------------------------------------------") |
| 69 | + makeRequest(client, jsonURL) |
| 70 | + fmt.Println() |
| 71 | + |
| 72 | + // Example 3: Simulate tier 1 eviction/restart |
| 73 | + fmt.Println("Example 3: Simulating tier 1 cache clear") |
| 74 | + fmt.Println("-----------------------------------------") |
| 75 | + fmt.Println("Clearing memory cache...") |
| 76 | + memoryCache.Delete(cacheKey(jsonURL)) |
| 77 | + fmt.Println("Making request (should hit tier 2 and promote to tier 1)...") |
| 78 | + makeRequest(client, jsonURL) |
| 79 | + fmt.Println() |
| 80 | + |
| 81 | + // Example 4: Make another request to verify promotion |
| 82 | + fmt.Println("Example 4: Verify promotion to tier 1") |
| 83 | + fmt.Println("--------------------------------------") |
| 84 | + fmt.Println("Making request (should hit tier 1 again after promotion)...") |
| 85 | + makeRequest(client, jsonURL) |
| 86 | + fmt.Println() |
| 87 | + |
| 88 | + // Example 5: Different URL to demonstrate independent caching |
| 89 | + fmt.Println("Example 5: Different URL") |
| 90 | + fmt.Println("-------------------------") |
| 91 | + makeRequest(client, "https://httpbin.org/headers") |
| 92 | + fmt.Println() |
| 93 | + |
| 94 | + fmt.Println("Examples completed!") |
| 95 | + fmt.Println("\nKey Takeaways:") |
| 96 | + fmt.Println("• First request: Cache miss → stores in all tiers") |
| 97 | + fmt.Println("• Second request: Cache hit from tier 1 (fastest)") |
| 98 | + fmt.Println("• After tier 1 clear: Hit from tier 2, auto-promoted to tier 1") |
| 99 | + fmt.Println("• Subsequent requests: Fast hits from tier 1 again") |
| 100 | + fmt.Println("• Each URL is cached independently across all tiers") |
| 101 | +} |
| 102 | + |
| 103 | +func makeRequest(client *http.Client, url string) { |
| 104 | + start := time.Now() |
| 105 | + |
| 106 | + resp, err := client.Get(url) |
| 107 | + if err != nil { |
| 108 | + log.Printf("Error: %v", err) |
| 109 | + return |
| 110 | + } |
| 111 | + defer resp.Body.Close() |
| 112 | + |
| 113 | + // Read and discard body to trigger caching |
| 114 | + _, _ = io.Copy(io.Discard, resp.Body) |
| 115 | + |
| 116 | + elapsed := time.Since(start) |
| 117 | + |
| 118 | + // Check cache status from header |
| 119 | + cacheStatus := "MISS" |
| 120 | + if resp.Header.Get("X-From-Cache") == "1" { |
| 121 | + cacheStatus = "HIT" |
| 122 | + } |
| 123 | + |
| 124 | + fmt.Printf("URL: %s\n", url) |
| 125 | + fmt.Printf("Status: %d\n", resp.StatusCode) |
| 126 | + fmt.Printf("Cache: %s\n", cacheStatus) |
| 127 | + fmt.Printf("Time: %v\n", elapsed) |
| 128 | +} |
| 129 | + |
| 130 | +// cacheKey generates the cache key for a URL |
| 131 | +// This is a simplified version - the actual Transport uses a more sophisticated key |
| 132 | +func cacheKey(url string) string { |
| 133 | + return url |
| 134 | +} |
0 commit comments