Skip to content

Commit

Permalink
Reorder example for readability (#18)
Browse files Browse the repository at this point in the history
* Reordering example for readability
* Renaming "Benefits" to "Features" in readme
  • Loading branch information
kevburnsjr authored Oct 2, 2019
1 parent dafbc3c commit b6ab5e1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}

Expand Down Expand Up @@ -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

Expand Down
56 changes: 28 additions & 28 deletions examples/basic/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
}

0 comments on commit b6ab5e1

Please sign in to comment.