Skip to content

Instantly share code, notes, and snippets.

@smallnest
Forked from fiorix/groupcache.go
Created July 31, 2019 08:27
Show Gist options
  • Save smallnest/51c7c6bf428f139db1ceb9f398457f5c to your computer and use it in GitHub Desktop.
Save smallnest/51c7c6bf428f139db1ceb9f398457f5c to your computer and use it in GitHub Desktop.

Revisions

  1. @fiorix fiorix revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion groupcache.go
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Group cache example: https://github.com/golang/groupcache
    // Simple groupcache example: https://github.com/golang/groupcache
    // Running 3 instances:
    // go run groupcache.go -addr=:8080 -pool=http://127.0.0.1:8080,http://127.0.0.1:8081,http://127.0.0.1:8082
    // go run groupcache.go -addr=:8081 -pool=http://127.0.0.1:8081,http://127.0.0.1:8080,http://127.0.0.1:8082
  2. @fiorix fiorix renamed this gist Mar 18, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions main.go → groupcache.go
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    // Group cache example: https://github.com/golang/groupcache
    // Running 3 instances:
    // go run main.go -addr=:8080 -pool=http://127.0.0.1:8080,http://127.0.0.1:8081,http://127.0.0.1:8082
    // go run main.go -addr=:8081 -pool=http://127.0.0.1:8081,http://127.0.0.1:8080,http://127.0.0.1:8082
    // go run main.go -addr=:8082 -pool=http://127.0.0.1:8082,http://127.0.0.1:8080,http://127.0.0.1:8081
    // go run groupcache.go -addr=:8080 -pool=http://127.0.0.1:8080,http://127.0.0.1:8081,http://127.0.0.1:8082
    // go run groupcache.go -addr=:8081 -pool=http://127.0.0.1:8081,http://127.0.0.1:8080,http://127.0.0.1:8082
    // go run groupcache.go -addr=:8082 -pool=http://127.0.0.1:8082,http://127.0.0.1:8080,http://127.0.0.1:8081
    // Testing:
    // curl localhost:8080/color?name=red
    package main
  3. @fiorix fiorix renamed this gist Mar 18, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @fiorix fiorix created this gist Mar 18, 2016.
    57 changes: 57 additions & 0 deletions groupcache-example.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    // Group cache example: https://github.com/golang/groupcache
    // Running 3 instances:
    // go run main.go -addr=:8080 -pool=http://127.0.0.1:8080,http://127.0.0.1:8081,http://127.0.0.1:8082
    // go run main.go -addr=:8081 -pool=http://127.0.0.1:8081,http://127.0.0.1:8080,http://127.0.0.1:8082
    // go run main.go -addr=:8082 -pool=http://127.0.0.1:8082,http://127.0.0.1:8080,http://127.0.0.1:8081
    // Testing:
    // curl localhost:8080/color?name=red
    package main

    import (
    "errors"
    "flag"
    "log"
    "net/http"
    "strings"

    "github.com/golang/groupcache"
    )

    var Store = map[string][]byte{
    "red": []byte("#FF0000"),
    "green": []byte("#00FF00"),
    "blue": []byte("#0000FF"),
    }

    var Group = groupcache.NewGroup("foobar", 64<<20, groupcache.GetterFunc(
    func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
    log.Println("looking up", key)
    v, ok := Store[key]
    if !ok {
    return errors.New("color not found")
    }
    dest.SetBytes(v)
    return nil
    },
    ))

    func main() {
    addr := flag.String("addr", ":8080", "server address")
    peers := flag.String("pool", "http://localhost:8080", "server pool list")
    flag.Parse()
    http.HandleFunc("/color", func(w http.ResponseWriter, r *http.Request) {
    color := r.FormValue("name")
    var b []byte
    err := Group.Get(nil, color, groupcache.AllocatingByteSliceSink(&b))
    if err != nil {
    http.Error(w, err.Error(), http.StatusNotFound)
    return
    }
    w.Write(b)
    w.Write([]byte{'\n'})
    })
    p := strings.Split(*peers, ",")
    pool := groupcache.NewHTTPPool(p[0])
    pool.Set(p...)
    http.ListenAndServe(*addr, nil)
    }