From b6ab5e11ab9a33c9dd1b14cc4dc3ecf02b84bf51 Mon Sep 17 00:00:00 2001 From: Kevin Burns Date: Wed, 2 Oct 2019 16:09:23 -0700 Subject: [PATCH] Reorder example for readability (#18) * Reordering example for readability * Renaming "Benefits" to "Features" in readme --- README.md | 50 +++++++++++++++++----------------- examples/basic/example.go | 56 +++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 8d7ac84..2cf75a2 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,30 @@ import ( "github.com/kevburnsjr/microcache" ) +func main() { + cache := microcache.New(microcache.Config{ + Nocache: true, + Timeout: 3 * time.Second, + TTL: 30 * time.Second, + StaleIfError: 3600 * time.Second, + StaleRecache: true, + StaleWhileRevalidate: 30 * time.Second, + CollapsedForwarding: true, + HashQuery: true, + QueryIgnore: []string{}, + Exposed: true, + SuppressAgeHeader: false, + Monitor: microcache.MonitorFunc(5*time.Second, logStats), + Driver: microcache.NewDriverLRU(1e4), + Compressor: microcache.CompressorSnappy{}, + }) + defer cache.Stop() + + h := cache.Middleware(handler{}) + + http.ListenAndServe(":80", h) +} + type handler struct { } @@ -77,33 +101,9 @@ func logStats(stats microcache.Stats) { stats.Errors, ) } - -func main() { - cache := microcache.New(microcache.Config{ - Nocache: true, - Timeout: 3 * time.Second, - TTL: 30 * time.Second, - StaleIfError: 3600 * time.Second, - StaleRecache: true, - StaleWhileRevalidate: 30 * time.Second, - CollapsedForwarding: true, - HashQuery: true, - QueryIgnore: []string{}, - Exposed: true, - SuppressAgeHeader: false, - Monitor: microcache.MonitorFunc(5*time.Second, logStats), - Driver: microcache.NewDriverLRU(1e4), - Compressor: microcache.CompressorSnappy{}, - }) - defer cache.Stop() - - h := cache.Middleware(handler{}) - - http.ListenAndServe(":80", h) -} ``` -## Benefits +## Features May improve service efficiency by reducing origin read traffic diff --git a/examples/basic/example.go b/examples/basic/example.go index 062a7f5..4df1b12 100644 --- a/examples/basic/example.go +++ b/examples/basic/example.go @@ -9,34 +9,6 @@ import ( "github.com/kevburnsjr/microcache" ) -type handler struct { -} - -var body = bytes.Repeat([]byte("1234567890"), 1e3) - -// This example fills up to 1.2GB of memory, so at least 2.0GB of RAM is recommended -func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - - // Enable cache - w.Header().Set("microcache-cache", "1") - - // Return a 10 kilobyte response body - w.Write(body) -} - -func logStats(stats microcache.Stats) { - total := stats.Hits + stats.Misses + stats.Stales - log.Printf("Size: %d, Total: %d, Hits: %d, Misses: %d, Stales: %d, Backend: %d, Errors: %d\n", - stats.Size, - total, - stats.Hits, - stats.Misses, - stats.Stales, - stats.Backend, - stats.Errors, - ) -} - func main() { // - Nocache: true // Cache is disabled for all requests by default @@ -120,3 +92,31 @@ func main() { http.ListenAndServe(":80", h) } + +type handler struct { +} + +var body = bytes.Repeat([]byte("1234567890"), 1e3) + +// This example fills up to 1.2GB of memory, so at least 2.0GB of RAM is recommended +func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + + // Enable cache + w.Header().Set("microcache-cache", "1") + + // Return a 10 kilobyte response body + w.Write(body) +} + +func logStats(stats microcache.Stats) { + total := stats.Hits + stats.Misses + stats.Stales + log.Printf("Size: %d, Total: %d, Hits: %d, Misses: %d, Stales: %d, Backend: %d, Errors: %d\n", + stats.Size, + total, + stats.Hits, + stats.Misses, + stats.Stales, + stats.Backend, + stats.Errors, + ) +}