func (ps *pubsub) connectRedis() error { ps.lock.Lock() defer ps.lock.Unlock() log.Println("pubsub connect redis") redisPub, err := goredis.Dial(&goredis.DialConfig{ Address: ps.opts.RedisPubAddress}) if err != nil { log.Fatal("Unable to connect to redis pub", err) return err } ps.redisPub = redisPub redisSub, err := goredis.Dial(&goredis.DialConfig{ Address: ps.opts.RedisSubAddress}) if err != nil { log.Fatal("Unable to connect to redis sub", err) return err } ps.redisSub = redisSub redisSubscriber, err := redisSub.PubSub() if err != nil { log.Fatal("Unable to create redis subscriber", err) return err } ps.redisSubscriber = redisSubscriber return nil }
func main() { ElasticClient, err := elastic.NewClient() if err != nil { fmt.Println("ES start error: ", err) } else { fmt.Println("Connection to ES server established successfully...") } RedisClient, err2 := goredis.Dial(&goredis.DialConfig{Address: "0.0.0.0:6379"}) if err2 != nil { fmt.Println("Redis start error: ", err) } else { fmt.Println("Connection to redis-server established successfully...") } file, _ := os.OpenFile("index.txt", os.O_APPEND|os.O_WRONLY, 0666) messages := make(chan string) go func() { for { FetchFromRedis(messages, RedisClient) } }() for { select { case val := <-messages: file.WriteString(string(val) + '\n') val = StoreDistance(val) Store2ES(ElasticClient, file, val) } } }
func supercars_05_delete(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric c := req.NewDefaultClient() base := s["supercars_url"].(string) b := gg.NewBracket("05_01_supercars_delete") redis, err := goredis.Dial(&goredis.DialConfig{Address: s["redis_srv"].(string)}) if err != nil { // is the redis server running? correct address? m.Error += err.Error() return } id, err := redis.SPop("supercars") if err != nil { // probably run out of data - so it does not make sense to continue m.Error += err.Error() return } r, err := http.NewRequest("DELETE", base+"/rest/supercars/"+string(id), nil) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { _, _, mm = req.DoRaw(c, r, m) } b.End(mm) }
func New(conf *configReader.Config) *redis.Redis { conn, connErr := redis.Dial(&redis.DialConfig{Address: conf.RedisAddress}) if connErr != nil { panic(connErr) } return conn }
func supercars_03_create(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric var resp map[string]interface{} c := req.NewDefaultClient() base := s["supercars_url"].(string) newCar := map[string]interface{}{"name": "Ferrari Enzo", "country": "Italy", "top_speed": "218", "0-60": "3.4", "power": "650", "engine": "5998", "weight": "1365", "description": "The Enzo Ferrari is a 12 cylinder " + "mid-engine berlinetta named after the company's founder, Enzo Ferrari.", "image": "050.png"} b := gg.NewBracket("03_01_supercars_create") r, err := req.NewPostJsonRequest(base+"/rest/supercars/", newCar) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { resp, _, mm = req.DoJson(c, r, m) id := resp["_id"].(string) if i, err := strconv.Atoi(id); err != nil || i <= RECORDS { m.Error += "Error: something went wrong during new record creation!" } else { redis, err := goredis.Dial( &goredis.DialConfig{Address: s["redis_srv"].(string)}) if err != nil { // is the redis server running? correct address? m.Error += err.Error() } else { redis.SAdd("supercars", id) // no way this can go wrong! } } } b.End(mm) }
func DeadHostWatch(host string, deadOut chan<- Event, deadQuit <-chan bool) { for { select { case <-deadQuit: fmt.Println("Dead Host Watch Quit Message.......") close(deadOut) return default: time.Sleep(1000 * time.Millisecond) fmt.Printf("Dead Host Watch check....%s\n", host) client, err := goredis.Dial(&goredis.DialConfig{Address: host}) if err != nil { log.Printf("Error connection to database %s", err.Error()) continue } log.Printf("Dead Host Ping check to database %s", host) if err := client.Ping(); err != nil { log.Printf("FALSE Ping check to database") continue } e := Event("DeadHostConnectOk") deadOut <- e client.ClosePool() } } }
func dial() *goredis.Redis { dialConfig := goredis.DialConfig{ Address: "avialeta-cache:6379", } client, err := goredis.Dial(&dialConfig) if err != nil { log.Error.Fatal(err) } return client }
func storeinRadis(message string, addr string) { client, err := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"}) reply, err2 := client.ExecuteCommand("SET", addr, message) err1 := reply.OKValue() fmt.Println(err1) fmt.Println(err) fmt.Println(err2) fmt.Println(reply) }
func main() { client, _ := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"}) for { reply, _ := client.RPop("myqueue1") if string(reply) != "" { storeinDB(string(reply)) fmt.Println(string(reply)) } } }
func Connect(masterConfig *goredis.DialConfig, slaveConfig *goredis.DialConfig) (*Redis, error) { r := &Redis{} log.Println("server connect redis") redisMaster, err := goredis.Dial(masterConfig) if err != nil { log.Fatal("Unable to connect to redis master", err) return r, err } r.redisMaster = redisMaster redisSlave, err := goredis.Dial(slaveConfig) if err != nil { log.Fatal("Unable to connect to redis sub", err) return r, err } r.redisSlave = redisSlave return r, err }
func NewRedis(address string) (*Redis, error) { var ( err error ) r := new(Redis) r.Client, err = goredis.Dial(&goredis.DialConfig{ Address: address, }) return r, err }
func redisHeartBeatchecker(host string) (*goredis.Redis, error) { client, err := goredis.Dial(&goredis.DialConfig{Address: host}) if err != nil { log.Printf("Error connection to database %s", host) log.Printf("Error connection to database->> %s", err.Error()) return client, err } if err := client.Ping(); err != nil { log.Printf("FALSE Ping check to database") return client, err } client.ClosePool() return client, nil }
func storeinRedis(res Response) { client, _ := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"}) res2B, _ := json.Marshal(res) fmt.Println("res2B", string(res2B)) reply, _ := client.LPush("Rqueue", string(res2B)) fmt.Println("Reply: ", reply) /* fmt.Println("Stored to Redis Hash...") fmt.Println(err) fmt.Println(err2) fmt.Println(reply) */ }
func redisGet(w http.ResponseWriter, r *http.Request) { cluster := r.FormValue("cluster") key := r.FormValue("key") redis, err := goredis.Dial(&goredis.DialConfig{Address: fmt.Sprintf("raftis-%s:%d", cluster, 6379)}) if err != nil { w.Write([]byte(err.Error())) return } val, err := redis.Get(key) if err != nil { w.Write([]byte(err.Error())) return } w.Write([]byte(val)) return }
func setSlaveOf(host string) error { client, err := goredis.Dial(&goredis.DialConfig{Address: host}) if err != nil { log.Printf("Error SlaveOf connection to database : %s", host) return err } redis_host := strings.Split(host, ":") if ret := client.SlaveOf(redis_host[0], redis_host[1]); ret != nil { log.Printf("SlaveOf Error %s", host) return ret } log.Printf("OK SlaveOf->> %s", host) client.ClosePool() return nil }
func main() { db, err := sql.Open("mysql", "root:@/go_db") client, err2 := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"}) if err != nil { fmt.Println(err) } else { fmt.Println("Database Connection established...") } if err2 != nil { fmt.Println(err2) } else { fmt.Println("Redis Connection established...") } fetchFromRedis(db, client) defer db.Close() }
func NewCache(addr, password, db string) (*Cache, error) { dbid, dbiderr := strconv.Atoi(db) if dbiderr != nil { return nil, dbiderr } config := &goredis.DialConfig{ Network: "tcp", Address: addr, Database: dbid, Password: password, Timeout: 10 * time.Second, MaxIdle: 100, } redisClient, err := goredis.Dial(config) if err != nil { return nil, err } return &Cache{redisClient: redisClient}, nil }
func watchServer(out chan<- Event, quit <-chan bool, watch chan *etcd.Response) { for { select { case <-quit: log.Printf(" WatchServer Quit Message..........") //close(out) default: time.Sleep(1000 * time.Millisecond) resp, _ := etcd_GetMasterData() _host := strings.Split(resp.Node.Value, "\n") log.Printf(" >>> WatchServer start >>> %s\n", resp.Node.Value) client, err := goredis.Dial(&goredis.DialConfig{Address: _host[0]}) if err != nil { info := config.GetServerInfo() key := fmt.Sprintf("%s/Dead", info.AppName) etcd_SetData(key, _host[0]) e := Event("MasterConnectErr") out <- e log.Printf("Error connection to database %s", err.Error()) continue } if err := client.Ping(); err != nil { info := config.GetServerInfo() key := fmt.Sprintf("%s/Dead", info.AppName) etcd_SetData(key, _host[0]) e := Event("MasterConnectErr") out <- e log.Printf("FALSE Ping check to database") continue } client.ClosePool() } } }
func supercars_04_update(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric //var resp map[string]interface{} c := req.NewDefaultClient() base := s["supercars_url"].(string) change := map[string]interface{}{"cylinders": "12", "name": "Ferrari Enzo", "country": "Italy", "top_speed": "218", "0-60": "3.4", "power": "650", "engine": "5998", "weight": "1365", "description": "The Enzo Ferrari " + "is a 12 cylinder mid-engine berlinetta named after the company's " + "founder, Enzo Ferrari.", "image": "050.png"} b := gg.NewBracket("04_01_supercars_update") redis, err := goredis.Dial(&goredis.DialConfig{Address: s["redis_srv"].(string)}) if err != nil { // is the redis server running? correct address? m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { id, err := redis.SPop("supercars") if err != nil { // probably run out of data - so it does not make sense to continue m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { r, err := req.NewPutJsonRequest(base+"/rest/supercars/"+string(id), change) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { _, _, mm = req.DoJson(c, r, m) //tsUpdate(m, c, base + "/rest/supercars/" + string(id), change) // add the record back! redis.SAdd("supercars", string(id)) // no way this can go wrong! } } } b.End(mm) }
func main() { client, err := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"}) for { fmt.Println("\033[H\033[2J") fmt.Println("Data in Redis Key Value Pair\n============================================================================\n") keys, err2 := client.ExecuteCommand("KEYS", "*") length := len(keys.Multi) for i := 0; i < length; i++ { key := string(keys.Multi[i].Bulk) value, err3 := client.Get(key) fmt.Println("Key:\t", string(key), "Value:\t", string(value)) Printerror(err) Printerror(err2) Printerror(err3) } time.Sleep(25 * time.Millisecond) } }
func storeinRadis(msg string) { client, _ := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"}) reply, _ := client.ExecuteCommand("LPUSH", "myqueue1", msg) fmt.Println(reply) }