[go: nahoru, domu]

Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
nqv committed Nov 2, 2017
1 parent 62c5a52 commit 0d64ef4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,10 @@ import (
"github.com/goburrow/cache"
)

func load(k cache.Key) (cache.Value, error) {
return fmt.Sprintf("%d", k), nil
}

func report(c cache.Cache) {
st := &cache.Stats{}
c.Stats(st)
fmt.Println(st)
}

func main() {
load := func(k cache.Key) (cache.Value, error) {
return fmt.Sprintf("%d", k), nil
}
// Create a new cache
c := cache.NewLoadingCache(load,
cache.WithMaximumSize(1000),
Expand All @@ -58,7 +51,9 @@ func main() {
case <-getTicker:
_, _ = c.Get(rand.Intn(2000))
case <-reportTicker:
report(c)
st := cache.Stats{}
c.Stats(&st)
fmt.Printf("%+v\n", st)
}
}
}
Expand Down
22 changes: 8 additions & 14 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@ import (
"github.com/goburrow/cache"
)

func load(k cache.Key) (cache.Value, error) {
return fmt.Sprintf("%d", k), nil
}

func report(c cache.Cache) {
st := &cache.Stats{}
c.Stats(st)

fmt.Printf("total: %d, hits: %d (%.2f%%), misses: %d (%.2f%%), evictions: %d, load: %s (%s)\n",
st.RequestCount(), st.HitCount, st.HitRate()*100.0, st.MissCount, st.MissRate()*100.0,
st.EvictionCount, st.TotalLoadTime, st.AverageLoadPenalty())
}

func main() {
load := func(k cache.Key) (cache.Value, error) {
return fmt.Sprintf("%d", k), nil
}
// Create a new cache
c := cache.NewLoadingCache(load,
cache.WithMaximumSize(1000),
Expand All @@ -36,7 +26,11 @@ func main() {
case <-getTicker:
_, _ = c.Get(rand.Intn(2000))
case <-reportTicker:
report(c)
st := cache.Stats{}
c.Stats(&st)
fmt.Printf("total: %d, hits: %d (%.2f%%), misses: %d (%.2f%%), evictions: %d, load: %s (%s)\n",
st.RequestCount(), st.HitCount, st.HitRate()*100.0, st.MissCount, st.MissRate()*100.0,
st.EvictionCount, st.TotalLoadTime, st.AverageLoadPenalty())
}
}
}

0 comments on commit 0d64ef4

Please sign in to comment.