Exemplo n.º 1
0
func ExampleString() {
	c, err := dial()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	c.Do("SET", "hello", "world")
	s, err := redis.String(c.Do("GET", "hello"))
	fmt.Printf("%#v\n", s)
	// Output:
	// "world"
}
Exemplo n.º 2
0
Arquivo: api.go Projeto: the42/ogdat
func (a *analyser) GetSortedSet(key string) func(request *restful.Request, response *restful.Response) {

	return func(request *restful.Request, response *restful.Response) {
		getentity := request.QueryParameter("id")
		sortorder := request.QueryParameter("sortorder")

		var entity string
		nums := -1

		var reply []interface{}
		var err error

		resultset := make([]IDNums, 0)

		rcon := a.pool.Get()
		defer rcon.Close()

		if len(getentity) > 0 {
			snums, err := redis.String(rcon.Do("ZSCORE", key, getentity))
			if err != nil {
				response.WriteError(http.StatusInternalServerError, err)
				return
			}
			if len(snums) > 0 {
				if i, err := strconv.ParseInt(snums, 10, 0); err == nil {
					resultset = append(resultset, IDNums{ID: getentity, Numsets: int(i)})
				}
			}
		} else {
			if sortorder == "asc" {
				reply, err = redis.Values(rcon.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
			} else {
				reply, err = redis.Values(rcon.Do("ZREVRANGE", key, 0, -1, "WITHSCORES"))
			}
			if err != nil {
				response.WriteError(http.StatusInternalServerError, err)
				return
			}

			for len(reply) > 0 {
				reply, err = redis.Scan(reply, &entity, &nums)
				if err != nil {
					response.WriteError(http.StatusInternalServerError, err)
					return
				}
				resultset = append(resultset, IDNums{ID: entity, Numsets: nums})
			}
		}
		response.WriteEntity(resultset)
	}
}
Exemplo n.º 3
0
// This example implements ZPOP as described at
// http://redis.io/topics/transactions using WATCH/MULTI/EXEC and scripting.
func Example_zpop() {
	c, err := dial()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer c.Close()

	// Add test data using a pipeline.

	for i, member := range []string{"red", "blue", "green"} {
		c.Send("ZADD", "zset", i, member)
	}
	if _, err := c.Do(""); err != nil {
		fmt.Println(err)
		return
	}

	// Pop using WATCH/MULTI/EXEC

	v, err := zpop(c, "zset")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(v)

	// Pop using a script.

	v, err = redis.String(zpopScript.Do(c, "zset"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(v)

	// Output:
	// red
	// blue
}