Exemplo n.º 1
0
func loop(queueName string, done chan struct{}, stop chan struct{}, wg *sync.WaitGroup) {
	defer wg.Done()
	memc, err := gomemcache.Connect(*queueHost, *queuePort)
	defer memc.Close()
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("Get loop started")
	for {
		select {
		case <-stop:
			log.Println("Stopped")
			return
		default:
			for i := 0; i < 10000; i++ {
				_, _, err := memc.Get(queueName)
				if err != nil {
					log.Println(err)
					done <- struct{}{}
					return
				}
			}
		}
	}
}
Exemplo n.º 2
0
func loop(queueName string, done chan struct{}, wg *sync.WaitGroup) {
	defer wg.Done()
	memc, err := gomemcache.Connect(*queueHost, *queuePort)
	defer memc.Close()
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("Set loop started")
	var value string
	for {
		select {
		case <-done:
			log.Println("Done")
			return
		default:
			for i := 0; i < 10000; i++ {
				value = strconv.Itoa(i)
				memc.Set(queueName, []byte(value), 0, 0)
				if err != nil {
					log.Println(err)
					return
				}
			}
		}
	}
}
Exemplo n.º 3
0
func NewMemcache(host string, port int) Cacher {
	serv, err := gomemcache.Connect(host, port)
	panik.On(err)

	m := memCache{
		mc: serv,
	}
	return m
	// TODO: close port on destruction
}
Exemplo n.º 4
0
func main() {
	memc, err := gomemcache.Connect("127.0.0.1", 11211)
	if err != nil {
		panic(err)
	}
	err = memc.Set("foo", []uint8("bar"), 0, 0)
	if err != nil {
		panic(err)
	}
	val, fl, _ := memc.Get("foo")
	fmt.Printf("%s %d\n", val, fl)
}
Exemplo n.º 5
0
func worker(done chan struct{}, wg *sync.WaitGroup) {
	defer wg.Done()
	memc, err := gomemcache.Connect(*queueHost, *queuePort)
	defer memc.Close()
	if err != nil {
		log.Println(err)
	}

	setsRemaning := *numSets
	getsRemaning := *numGets
	getSetRatio := float32(*numGets) / float32(*numSets)
	dataSource := &dataSource{make([]byte, *itemSize), randbo.New()}

	for {
		if getsRemaning > 0 {
			if setsRemaning < 1 {
				err = get(memc)
				getsRemaning--
			} else if float32(getsRemaning)/float32(setsRemaning) > getSetRatio {
				err = get(memc)
				getsRemaning--
			} else {
				err = set(memc, dataSource)
				setsRemaning--
			}
		} else if setsRemaning > 0 {
			err = set(memc, dataSource)
			setsRemaning--
		} else {
			return
		}
		if err != nil && err.Error() != "memcache: not found" {
			log.Println(err)
			memc, err = gomemcache.Connect(*queueHost, *queuePort)
			if err != nil {
				log.Println(err)
			}
		}
	}
}
Exemplo n.º 6
0
func main() {
	// If you want to use this with UNIX domain socket, you can use like a following source code.
	// On a UNIX domain socket, port is 0.
	// mc, err := gomemcache.Connect("/path/to/memcached.sock", 0)
	memc, err := gomemcache.Connect("127.0.0.1", 11211)
	if err != nil {
		panic(err)
	}
	err = memc.Set("foo", []uint8("bar"), 0, 0)
	if err != nil {
		panic(err)
	}
	val, fl, _ := memc.Get("foo")
	fmt.Printf("%s %d\n", val, fl)
}