mirror of
https://github.com/mailgun/groupcache.git
synced 2024-10-02 21:02:08 +00:00
Merge pull request #13 from mailgun/thrawn/develop
Removed tabs from README example
This commit is contained in:
commit
ba255c644a
71
README.md
71
README.md
@ -31,9 +31,6 @@ For API docs and examples, see http://godoc.org/github.com/mailgun/groupcache
|
|||||||
However this does require that time on all nodes in the cluster is synchronized
|
However this does require that time on all nodes in the cluster is synchronized
|
||||||
for consistent expiration of values.
|
for consistent expiration of values.
|
||||||
|
|
||||||
* Network methods now accept golang standard `context.Context` instead of
|
|
||||||
`groupcache.Context`.
|
|
||||||
|
|
||||||
* Now always populating the hotcache. A more complex algorithm is unnecessary
|
* Now always populating the hotcache. A more complex algorithm is unnecessary
|
||||||
when the LRU cache will ensure the most used values remain in the cache. The
|
when the LRU cache will ensure the most used values remain in the cache. The
|
||||||
evict code ensures the hotcache never overcrowds the maincache.
|
evict code ensures the hotcache never overcrowds the maincache.
|
||||||
@ -84,12 +81,12 @@ In a nutshell, a groupcache lookup of **Get("foo")** looks like:
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mailgun/groupcache/v2"
|
"github.com/mailgun/groupcache/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleUsage() {
|
func ExampleUsage() {
|
||||||
@ -113,42 +110,42 @@ func ExampleUsage() {
|
|||||||
}()
|
}()
|
||||||
defer server.Shutdown(context.Background())
|
defer server.Shutdown(context.Background())
|
||||||
|
|
||||||
// Create a new group cache with a max cache size of 3MB
|
// Create a new group cache with a max cache size of 3MB
|
||||||
group := groupcache.NewGroup("users", 3000000, groupcache.GetterFunc(
|
group := groupcache.NewGroup("users", 3000000, groupcache.GetterFunc(
|
||||||
func(ctx context.Context, id string, dest groupcache.Sink) error {
|
func(ctx context.Context, id string, dest groupcache.Sink) error {
|
||||||
|
|
||||||
// Returns a protobuf struct `User`
|
// Returns a protobuf struct `User`
|
||||||
if user, err := fetchUserFromMongo(ctx, id); err != nil {
|
if user, err := fetchUserFromMongo(ctx, id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the user in the groupcache to expire after 5 minutes
|
// Set the user in the groupcache to expire after 5 minutes
|
||||||
if err := dest.SetProto(&user, time.Now().Add(time.Minute*5)); err != nil {
|
if err := dest.SetProto(&user, time.Now().Add(time.Minute*5)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
|
||||||
var user User
|
var user User
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err := group.Get(ctx, "12345", groupcache.ProtoSink(&user)); err != nil {
|
if err := group.Get(ctx, "12345", groupcache.ProtoSink(&user)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("-- User --\n")
|
fmt.Printf("-- User --\n")
|
||||||
fmt.Printf("Id: %s\n", user.Id)
|
fmt.Printf("Id: %s\n", user.Id)
|
||||||
fmt.Printf("Name: %s\n", user.Name)
|
fmt.Printf("Name: %s\n", user.Name)
|
||||||
fmt.Printf("Age: %d\n", user.Age)
|
fmt.Printf("Age: %d\n", user.Age)
|
||||||
fmt.Printf("IsSuper: %t\n", user.IsSuper)
|
fmt.Printf("IsSuper: %t\n", user.IsSuper)
|
||||||
|
|
||||||
// Remove the key from the groupcache
|
// Remove the key from the groupcache
|
||||||
if err := group.Remove(ctx, "12345"); err != nil {
|
if err := group.Remove(ctx, "12345"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user