Exemplo n.º 1
0
func main() {

	// 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))
}
Exemplo n.º 2
0
func RedisPush(rawtokenstring string) int {
	spec := redis.DefaultSpec().Password("go-redis")
	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		fmt.Println("error creating client for: ", e)
	}
	defer client.Quit()
	//pidString := strconv.Itoa(cmdI.Process.Pid)
	decryptedToken, err := jwt.Parse(rawtokenstring, func(token *jwt.Token) (interface{}, error) {
		return []byte(secretKey), nil
	})
	//fmt.Printf("token strings\nRaw: [%s]\nHeader: [%s]\nSignature: [%s]\n", decryptedToken.Raw, decryptedToken.Header, decryptedToken.Signature)

	//check if no error and valid token
	if err == nil && decryptedToken.Valid {
		fmt.Println("token is valid and not expired")

	} else {
		fmt.Println("Not valid: ", err)
		return 0
	}

	userID := decryptedToken.Claims["id"].(string)
	//fmt.Println("redis func userid: " + userID + "\n redis func raw: " + decryptedToken.Raw)
	var buf bytes.Buffer
	buf.Write([]byte(decryptedToken.Raw))
	e = client.Hset("clients/token", userID, buf.Bytes())
	//to retrieve token in redis-cli, do hget clients/tokens legacy
	if e != nil {
		fmt.Println("error writing to list")
		return 0
	}
	return 1
}
Exemplo n.º 3
0
func NewClient(t *testing.T) redis.Client {
	client, err := redis.NewSynchClientWithSpec(getTestConnSpec())
	if err != nil {
		t.Fatalf("TestGet - ", err)
	}
	return client
}
Exemplo n.º 4
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.(redis.RedisClient).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
}
Exemplo n.º 5
0
func getclient() redis.Client {
	spec := redis.DefaultSpec().Db(1)
	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		log.Fatal(e)
	}
	return client
}
Exemplo n.º 6
0
/// 创建新的日志记录器.
///
/// @param servAddr redis服务器地址
/// @param servPort redis服务器端口
/// @param logLev 限制日志输出级别
/// @return 成功则返回日志记录器,同时error为nil.
func NewLogger(servAddr string, servPort int, logLev uint8) (*Logger, error) {
	spec := redis.DefaultSpec().Host(servAddr).Port(servPort)
	cli, err := redis.NewSynchClientWithSpec(spec)

	if nil != err {
		return nil, err
	}

	return &Logger{logLev, servAddr, servPort, cli, new(defaultFormatter)}, nil
}
Exemplo n.º 7
0
func NewReportInfo(mid string, mt string, intv int16, reportAddr string, reportPort int) (*ReportInfo, error) {

	spec := redis.DefaultSpec().Host(reportAddr).Port(reportPort)
	cli, err := redis.NewSynchClientWithSpec(spec)

	if err != nil {
		return nil, err
	}

	return &ReportInfo{mid, mt, intv, cli}, nil
}
Exemplo n.º 8
0
// Check that connection is actually passing passwords from spec
func TestSyncClientConnectWithSpec(t *testing.T) {
	spec := getTestConnSpec()

	client, err := redis.NewSynchClientWithSpec(spec)
	if err != nil {
		t.Fatalf("failed to create client with spec. Error: %s", err)
	} else if client == nil {
		t.Fatal("BUG: client is nil")
	}
	client.Quit()
}
Exemplo n.º 9
0
// Check that connection is actually passing passwords from spec
// and catching AUTH ERRs.
func TestSyncClientConnectWithBadSpec(t *testing.T) {
	spec := getTestConnSpec()
	spec.Password("bad-password")
	client, expected := redis.NewSynchClientWithSpec(spec)
	if expected == nil {
		t.Error("BUG: Expected a RedisError")
	}
	if client != nil {
		t.Error("BUG: sync client reference on error MUST be nil")
	}
}
Exemplo n.º 10
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
}
Exemplo n.º 11
0
func makeConcurrentClients(workers int) (clients []redis.Client, err os.Error) {
    clients = make([]redis.Client, workers);
    for i := 0; i < workers; i++ {
		spec := redis.DefaultSpec().Db(13);
		client, e := redis.NewSynchClientWithSpec (spec);
		if e != nil {
			log.Stderr ("Error creating client for worker: ", e);
			return nil, e;
		}
		clients[i] = client;
    }
    return;
}
Exemplo n.º 12
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.RedisClient().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
}
Exemplo n.º 13
0
func NewUniverse(dbNo int) *Universe {
	u := new(Universe)
	u.Players = make(map[int]*Player)
	u.Rooms = make(map[int]*Room)
	u.children = NewFlexContainer("Persistents", "TimeListeners")
	spec := redis.DefaultSpec().Db(dbNo)
	client, err := redis.NewSynchClientWithSpec(spec)
	if err != nil {
		panic(err)
	} else {
		u.dbConn = client
		u.Store = NewTinyDB(client)
	}
	return u
}
Exemplo n.º 14
0
func doOne(cnt int) os.Error {

	var delta int64
	spec := redis.DefaultSpec().Db(13)

	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!")
	}

	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)

	client.Quit()
	return nil
}
Exemplo n.º 15
0
func syncPublish(spec *redis.ConnectionSpec, channel string, messageType string) {

	client, err := redis.NewSynchClientWithSpec(spec)
	if err != nil {
		log.Errorf("Failed to create the redis client : %s", err)
		os.Exit(1)
	}

	msg := []byte(fmt.Sprintf("{id : %s }", messageType))
	rcvCnt, err := client.Publish(channel, msg)
	if err != nil {
		log.Errorf("Error to publish the messge to the redis : %s", err)
	} else {
		log.Debugf("Message published to %d subscribers", rcvCnt)
	}

	client.Quit()
}
Exemplo n.º 16
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()
}
Exemplo n.º 17
0
func Connect() *Connection {
	fullname := GetConfig("appname")
	database := GetConfig("database")
	db, err := strconv.Atoi(database)
	if err != nil {
		log.Stderrf("Can't read the database", err)
		return nil
	}
	spec := redis.DefaultSpec().Db(db)
	cli, err := redis.NewSynchClientWithSpec(spec)
	if err != nil {
		log.Stderrf("Can't connect to the database", err)
		return nil
	}
	err = cli.Ping()
	if err != nil {
		log.Stderrf("Can't ping the database", err)
		return nil
	}
	return &Connection{fullname, db, cli}
}
Exemplo n.º 18
0
func NewClient(cspec *ClientSpec) Client {
	if cspec == nil {
		cspec = DefaultSpec()
	}

	cspec.Queue = fmt.Sprintf("resque:queue:%s", cspec.Queue)

	spec := redis.DefaultSpec()
	spec.Host(strings.Split(cspec.RedisLocation, ":")[0])
	i, err := strconv.Atoi(strings.Split(cspec.RedisLocation, ":")[1])
	if err != nil {
		log.Panicln("Invalid port", cspec.RedisLocation, err)
	}
	spec.Port(i)
	client, e := redis.NewSynchClientWithSpec(spec)
	if e != nil {
		log.Panicln("failed to create the client", e)
	}
	return Client{
		cspec,
		client,
	}
}