Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple groupcache example
// Simple groupcache example:
// Running 3 instances:
// go run groupcache.go -addr=:8080 -pool=,,
// go run groupcache.go -addr=:8081 -pool=,,
// go run groupcache.go -addr=:8082 -pool=,,
// Testing:
// curl localhost:8080/color?name=red
package main
import (
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")
return nil
func main() {
addr := flag.String("addr", ":8080", "server address")
peers := flag.String("pool", "http://localhost:8080", "server pool list")
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)
p := strings.Split(*peers, ",")
pool := groupcache.NewHTTPPool(p[0])
http.ListenAndServe(*addr, nil)

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?

not bad Fiori, not bad :)

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

rbastic commented Dec 11, 2016

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

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