Beispiel #1
0
func delKey(key string, i *int32) {
	spec := redis.DefaultSpec()
	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		log.Println("failed to create the client", e)
		return
	}
	for {

		if *i > MAXKEYS/2 {
			s := strconv.Itoa(int(*i))
			key := key + s
			value, e := client.Get(key)
			if e != nil {
				log.Println("error on Get", e)
				return
			}
			if value != nil {
				client.Del(key)
				fmt.Println("Deleted " + key)
				atomic.AddInt32(i, -1)
				runtime.Gosched()
			}
		}
		time.Sleep(time.Millisecond * 100)
	}
}
Beispiel #2
0
func addKey(key string, new_value string, i *int32) {
	spec := redis.DefaultSpec()
	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		log.Println("failed to create the client", e)
		return
	}
	for {
		atomic.AddInt32(i, 1)
		if *i > MAXKEYS {
			done <- true
		} else {
			if *i%100000 == 0 {
				fmt.Printf("Inserted %d \n", *i)
			}
		}
		key := key + strconv.Itoa(int(*i))
		value, e := client.Get(key)
		if e != nil {
			log.Println("error on Get", e)
			return
		}
		if value == nil {
			value2set := []byte(new_value)
			client.Set(key, value2set)
			runtime.Gosched()
		} else {
			//fmt.Println("Already found " + string(key))
		}
		//time.Sleep(time.Millisecond * 10)
	}
}
Beispiel #3
0
// Reflect over the methods defined in redis.Client
// and send back as []string (tolowercase)
// TOOD get rid of redundant code in switch
// (REVU: needs minor update to core code)
func getDefinedMethods(ctype clientType) (map[string]string, *error_) {

	var mmap = map[string]string{}

	spec := redis.DefaultSpec().Db(13).Password("go-redis")

	var e redis.Error
	var client interface{}

	switch ctype {
	case sync:
		client, e = redis.NewSynchClientWithSpec(spec)
	case async:
		client, e = redis.NewAsynchClientWithSpec(spec)
	}
	if e != nil {
		log.Println("ignoring - ", e)
	}
	if client == nil {
		return mmap, &error_{"client is nil", nil}
	} else {
		defer client.Quit()
	}

	tc := reflect.TypeOf(client)
	nm := tc.NumMethod()

	for i := 0; i < nm; i++ {
		m := tc.Method(i)
		mname := strings.ToLower(m.Name)
		mmap[mname] = mname
	}
	return mmap, nil
}
Beispiel #4
0
func NewClient(t *testing.T) redis.Client {
	client, err := redis.NewSynchClientWithSpec(getTestConnSpec())
	if err != nil {
		t.Fatalf("TestGet - ", err)
	}
	return client
}
Beispiel #5
0
func makeConcurrentClients(workers int) (clients []redis.Client, err error) {
	clients = make([]redis.Client, workers)
	for i := 0; i < workers; i++ {
		spec := redis.DefaultSpec().Db(13).Password("go-redis")
		client, e := redis.NewSynchClientWithSpec(spec)
		if e != nil {
			log.Println("Error creating client for worker: ", e)
			return nil, e
		}
		clients[i] = client
	}
	return
}
Beispiel #6
0
func doOne(cnt int) error {

	var delta time.Duration
	spec := redis.DefaultSpec().Db(13).Password("go-redis")

	fmt.Printf("\n\n=== Bench synchclient ================ 1 Client -- %d opts --- \n", cnt)
	fmt.Println()

	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		return onError("on NewSynchClient call: ", e)
	}
	if client == nil {
		return failedTest("NewSynchClient returned nil!")
	}
	//	defer client.Quit()   // will be deprecated soon
	defer client.Quit()

	client.Flushdb()

	delta = doPing(client, cnt)
	report("PING", delta, cnt)

	delta = doIncr(client, cnt)
	report("INCR", delta, cnt)

	delta = doSet(client, cnt)
	report("SET", delta, cnt)

	delta = doGet(client, cnt)
	report("GET", delta, cnt)

	delta = doSadd(client, cnt)
	report("SADD", delta, cnt)

	delta = doLpush(client, cnt)
	report("LPUSH", delta, cnt)

	delta = doRpush(client, cnt)
	report("RPUSH", delta, cnt)

	delta = doLpop(client, cnt)
	report("LPOP", delta, cnt)

	delta = doRpop(client, cnt)
	report("RPOP", delta, cnt)

	return nil
}
Beispiel #7
0
func syncPublish(spec *redis.ConnectionSpec, channel string) {

	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		log.Println("failed to create the sync client", e)
		return
	}

	for i := 0; i < 100; i++ {
		msg := []byte(fmt.Sprintf("this is message # %d (using sync client)!", i))
		rcvCnt, err := client.Publish(channel, msg)
		if err != nil {
			fmt.Printf("Error on Publish - %s", err)
		} else {
			fmt.Printf("Message sent to %d subscribers\n", rcvCnt)
		}
	}

	client.Quit()
}
Beispiel #8
0
func main() {

	// Parse command-line flags; needed to let flags used by Go-Redis be parsed.
	flag.Parse()

	// create the client.  Here we are using a synchronous client.
	// Using the default ConnectionSpec, we are specifying the client to connect
	// to db 13 (e.g. SELECT 13), and a password of go-redis (e.g. AUTH go-redis)

	spec := redis.DefaultSpec().Db(13).Password("go-redis")
	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		log.Println("failed to create the client", e)
		return
	}

	key := "examples/hello/user.name"
	value, e := client.Get(key)
	if e != nil {
		log.Println("error on Get", e)
		return
	}

	if value == nil {
		fmt.Printf("\nHello, don't believe we've met before!\nYour name? ")
		reader := bufio.NewReader(os.Stdin)
		user, _ := reader.ReadString(byte('\n'))
		if len(user) > 1 {
			user = user[0 : len(user)-1]
			value = []byte(user)
			client.Set(key, value)
		} else {
			fmt.Printf("vafanculo!\n")
			return
		}
	}
	fmt.Printf("Hey, ciao %s!\n", fmt.Sprintf("%s", value))
}
Beispiel #9
0
func addSingleKey(key string, new_value string) {
	spec := redis.DefaultSpec()
	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		log.Println("failed to create the client", e)
		return
	}

	value, e := client.Get(key)
	if e != nil {
		log.Println("error on Get", e)
		return
	}
	if value == nil {
		value2set := []byte(new_value)
		client.Set(key, value2set)
		runtime.Gosched()
	} else {
		fmt.Println("Already found " + string(key))
	}
	//time.Sleep(time.Millisecond * 10)

}