Skip to content

Instantly share code, notes, and snippets.

@fiorix
Last active February 6, 2024 10:39
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save fiorix/816117cfc7573319b72d to your computer and use it in GitHub Desktop.
Save fiorix/816117cfc7573319b72d to your computer and use it in GitHub Desktop.
Simple groupcache example
// 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
// 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
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)
}
@lvhuat
Copy link

lvhuat commented Nov 23, 2016

What do the handler of /color mean, Do remote groupcahes will request the path for data? Or just a path for debuging?

@pkieltyka
Copy link

not bad Fiori, not bad :)

@pkieltyka
Copy link

the unfortunate thing about groupcache is that they're not willing to evolve it. I personally think there should be a default cache store which is just an in-memory lru .. that same in-memory lru that they use to actually cache results is what I'd like to use to store my app cacheable data, and not have to have one allocation for my store then group cache with its own allocation of the same data

Copy link

ghost commented Dec 11, 2016

Sorry, but you could explain a bit more about what you mean? Why do you want to do this?

@24vinay84
Copy link

HI.. I am unable to get the github.com/golang/groupcache.
How to get ?

@rranjan3
Copy link

You probably need to update to accommodate golang/groupcache#131. make use of the standard library's context.Context.

@cognusion
Copy link

line 45 should pass context.TODO() instead of nil, or it will panic on modern compilers.

@vallabhiaf
Copy link

Can we fetch the current set of peers in the pool if we are setting up peers dynamically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment