Exemple #1
0
func main() {

	var port = flag.String("port", "9001", "frontend port")
	var set = flag.Bool("set", false, "doing a set?")
	var get = flag.Bool("get", false, "doing a get?")
	var cget = flag.Bool("cget", false, "doing a get?")
	var key = flag.String("key", "foo", "key to get")
	var value = flag.String("value", "bar", "value to set")
	flag.Parse()

	client := new(client.Client)
	if *cget {
		client, err := rpc.DialHTTP("tcp", "localhost:"+*port)
		if err != nil {
			fmt.Printf("error %s", err)
		}
		args := &api.Load{*key}
		var reply api.ValueResult
		err = client.Call("Frontend.Get", args, &reply)
		if err != nil {
			fmt.Printf("error %s", err)
		}
		fmt.Println(string(reply.Value))
		return
	}

	if *get {
		var reply = client.Get(*key)
		fmt.Println(reply)
		return
	}

	if *set {
		client.Set(*key, *value)
		return
	}

	flag.PrintDefaults()
	return

}
func main() {

	var port = flag.String("port", "8001", "groupcache port")
	flag.Parse()

	peers := groupcache.NewHTTPPool("http://localhost:" + *port)

	client := new(client.Client)

	var stringcache = groupcache.NewGroup("SlowDBCache", 64<<20, groupcache.GetterFunc(
		func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
			result := client.Get(key)
			fmt.Printf("asking for %s from dbserver\n", key)
			dest.SetBytes([]byte(result))
			return nil
		}))

	peers.Set("http://localhost:8001", "http://localhost:8002", "http://localhost:8003")

	frontendServer := NewServer(stringcache)

	i, err := strconv.Atoi(*port)
	if err != nil {
		// handle error
		fmt.Println(err)
		os.Exit(2)
	}
	var frontEndport = ":" + strconv.Itoa(i+1000)
	go frontendServer.Start(frontEndport)

	fmt.Println(stringcache)
	fmt.Println("cachegroup slave starting on " + *port)
	fmt.Println("frontend starting on " + frontEndport)
	http.ListenAndServe("127.0.0.1:"+*port, http.HandlerFunc(peers.ServeHTTP))

}