Beispiel #1
0
func (this *MyMethod) Setkeyskv(key string, arr ...string) (interface{}, error) {
	method := "SetkeysKV"
	res, err := models.RedisRun("keys", key)
	if err != nil {
		return "Setkeyskv error", err
	}

	if resArr, ok := res.([]interface{}); ok {
		fmt.Println("len of keys is ", len(resArr))
		for i, val := range resArr {
			if str, ok := val.([]byte); ok {

				unitkey := string(str)
				res, err := models.RedisRun("hmset", unitkey, arr)
				if err != nil {
					return method + "errors", err
				}
				fmt.Printf(method+"id %-5d key %-60s. res is  %-30s\n", i, string(str), res)
			}
		}
	} else {
		return method + " failed", nil
	}
	return method + "success", nil
}
Beispiel #2
0
func (this *MyMethod) Delkeys(key string, _ ...string) (interface{}, error) {
	// method := "delkeys"
	res, err := models.RedisRun("keys", key)
	if err != nil {
		return "delkeys error", err
	}

	if resArr, ok := res.([]interface{}); ok {
		fmt.Println("len of keys is ", len(resArr))
		for i, val := range resArr {
			if str, ok := val.([]byte); ok {

				delkey := string(str)
				res, err := models.RedisRun("del", delkey)
				if err != nil {
					return "delkeys errors", err
				}
				fmt.Printf("delete id %-5d key %-60s. res is  %-30d\n", i, string(str), res)
			}
		}
	} else {
		fmt.Println("result: ", res)
	}
	return "delkeys success", nil
}
Beispiel #3
0
func (this *MyMethod) Getjson(key string, args ...string) (interface{}, error) {
	method := "SetkeysKV"
	if len(args) > 0 {
		return method + " error", fmt.Errorf("this command has none args.")
	}
	res, err := models.RedisRun("get", key)
	if err != nil {
		return "GetJson error", err
	}

	if resArr, ok := res.([]byte); ok {
		return string(resArr), nil
	} else {
		return method + " failed", nil
	}
}
Beispiel #4
0
func main() {
	fmt.Println("input redis command or self command,input help see detail.")
	for {
		fmt.Printf(">> ")

		cmd := ReadLine()
		switch strings.TrimSpace(cmd) {
		case "":
			continue
		case "help":
			showSelfCmd()
			continue
		case "exit":
		case "quit":
			return
		}

		arr := strings.Split(cmd, " ")
		for i, _ := range arr {
			arr[i] = strings.TrimSpace(arr[i])
		}

		var res interface{}
		var err error

		length := len(arr)
		m := MyMethod{}
		/*		if length <= 0 {
					continue
				} else if length == 1 {
					res, err = models.RedisOnlyCmd(arr[0])
				} else if length == 2 {
					res, err = models.RedisRun(arr[0], arr[1])
				} else {
					switch arr[0] {
					case "delkeys":
						fmt.Println("delete keys")
					default:
						res, err = models.RedisRunArgs(arr[0], arr[1], arr[2:])
					}
				}*/
		if length <= 0 {
			continue
		} else if m.IsFunc(arr[0]) {
			f, ok := m.GetFunc(arr[0])
			if !ok {
				fmt.Println("Error! Can't find the function.")
			}
			if length == 1 {
				fmt.Println("have not have one args function.")
			} else {
				//即使arr的长度只有2,那么这里是不会报错的。而且正常调用
				res, err = f(arr[1], arr[2:]...)
			}
		} else {
			if length == 1 {
				res, err = models.RedisOnlyCmd(arr[0])
			} else if length == 2 {
				res, err = models.RedisRun(arr[0], arr[1])
			} else if length == 3 {
				res, err = models.RedisRun(arr[0], arr[1], arr[2])
			} else {
				res, err = models.RedisRun(arr[0], arr[1], arr[2:])
			}
		}
		if err != nil {
			fmt.Println("request error", err)
		}

		if resArr, ok := res.([]interface{}); ok {
			fmt.Println("len of res is ", len(resArr))
			for j, val := range resArr {
				if str, ok := val.([]byte); ok {
					fmt.Println("res interface to string is :", j, string(str))
				}
				if strArr, ok := val.([]interface{}); ok {
					for i, v := range strArr {
						fmt.Println("res to array ", j, i, string(v.([]byte)))
					}

				}
			}
		} else {
			if strByte, ok := res.([]byte); ok {
				fmt.Println("res byte to string is :", string(strByte))
			} else {
				fmt.Println("res:")
				fmt.Println(res)
			}
		}
	}
}