S
S
sergey_fs2017-12-21 01:29:42
go
sergey_fs, 2017-12-21 01:29:42

Golang Redis get values?

Can you tell me I'm learning redis . Through cli I add the line . The list shows cout 1. Now I display the list through Go 0. And if I re-enter the cli, then the line is gone. Go code https://play.golang.org/p
----- With this script, I put data in Redis

import (
  "fmt"
  "time"
  "github.com/go-redis/redis"
)
var (
  client *redis.Client
  err error
)
///Структура для Redis
///{"client":"79095876084","dispach":"12548","timeout":"35","addr":"http.yandex.ru","session_id":"31345134"}
type data struct {
  Client string `json:"client"`
  Dispach string `json:"dispach"`
  Timeout string `json:"timeout"`
  Addr string `json:"addr"`
  Session_id string `json:"session_id"`
  Method string `json:"method"`
}
func init() {
  client = redis.NewClient(&redis.Options{
    Addr:         ":6379",
    DialTimeout:  10 * time.Second,
    ReadTimeout:  30 * time.Second,
    WriteTimeout: 30 * time.Second,
    PoolSize:     10,
    PoolTimeout:  30 * time.Second,
  })
  client.FlushDB()
}

func redis_connect() {
  client := redis.NewClient(&redis.Options{
    Addr:     "localhost:6379",
    Password: "", // no password set
    DB:       0,  // use default DB
  })
  pong, err := client.Ping().Result()
  fmt.Println(pong, err)
  if err != nil {fmt.Println("Redis is not Connect")}else {fmt.Println("Redis is Connected")}
  // Output: PONG <nil>
}
func main() {
  ////Подключение к БД Redis
  redis_connect()
  /*
  Получить длину списка
  dialer -- буфер на очередь в обзвон
   */
  /////Загружаю номер в список
  err := client.LPush("dialer","{\"client\":\"79095876084\",\"dispach\":\"12548\",\"timeout\":\"35\",\"addr\":\"http.yandex.ru\",\"session_id\":\"31345134\",\"method\":\"31345134\"}").Err()
  if err != nil {
    panic(err)
  }

  err = client.LPush("dialer","{\"client\":\"79094\",\"dispach\":\"12548\",\"timeout\":\"35\",\"addr\":\"http.yandex.ru\",\"session_id\":\"31345134\",\"method\":\"31345134\"}").Err()
  if err != nil {
    panic(err)
  }

  err = client.LPush("dialer","{\"client\":\"84\",\"dispach\":\"12548\",\"timeout\":\"35\",\"addr\":\"http.yandex.ru\",\"session_id\":\"31345134\",\"method\":\"31345134\"}").Err()
  if err != nil {
    panic(err)
  }


}

I take others
package main

import (
  "fmt"
  "time"
  "github.com/go-redis/redis"
  "encoding/json"
)
var (
  client *redis.Client
  err error
)
///Структура для Redis
///{"client":"79095876084","dispach":"12548","timeout":"35","addr":"http.yandex.ru","session_id":"31345134"}
type data struct {
  Client string `json:"client"`
  Dispach string `json:"dispach"`
  Timeout string `json:"timeout"`
  Addr string `json:"addr"`
  Session_id string `json:"session_id"`
    Method string `json:"method"`
  }
func init() {
  fmt.Println("Балансировшик очереди вызовов с Redis  + Сокет FreeSWITCH ")
  client = redis.NewClient(&redis.Options{
    Addr:         ":6379",
    DialTimeout:  10 * time.Second,
    ReadTimeout:  30 * time.Second,
    WriteTimeout: 30 * time.Second,
    PoolSize:     10,
    PoolTimeout:  30 * time.Second,
  })
  client.FlushDB()
}

func redis_connect() {
  client := redis.NewClient(&redis.Options{
    Addr:     "localhost:6379",
    Password: "", // no password set
    DB:       0,  // use default DB
  })
  pong, err := client.Ping().Result()
  fmt.Println(pong, err)
  if err != nil {fmt.Println("Redis is not Connect")}else {fmt.Println("Redis is Connected")}
  // Output: PONG <nil>
}
func main() {
////Подключение к БД Redis
  redis_connect()
  /*
  Получить длину списка
  dialer -- буфер на очередь в обзвон
   */

  len_icc , err := client.LLen("dialer").Result()
  if err != nil {
    panic(err)
  }
  /*
  Либа https://godoc.org/github.com/go-redis/redis#Ring.LLen
  https://github.com/go-redis/redis
   */
  fmt.Println("Количество строк в Списке Редис по услуге Автоматический обзвон клиентов", len_icc)
  int_len :=  int(len_icc)
  ///Прохожу все циклом
  for i:=0 ; i< int_len ; i++{
    i_int64 := int64(i)
    value_icc , err := client.LIndex("dialer",i_int64).Bytes()
    if err != nil {
      panic(err)
    }
    var icc_list data
    ////разбираю JSON
    err = json.Unmarshal(value_icc, &icc_list)
    if err != nil {
      fmt.Println("JSON ERR")
      return
    }
    fmt.Println("Адрес куда отправлять ответ ",icc_list.Addr,"Номер клиента",icc_list.Client,"Номер рассылки",icc_list.Dispach,"Timeout",icc_list.Timeout,"Session_ID",icc_list.Session_id,"Method",icc_list.Method)
  }
}

But after I execute the script that writes data to the database and after it I immediately start the script that checks for new data, it is empty
[[email protected] go]# go run test_redis.go
PONG
Redis is Connected
[[email protected] go]# redis-cli
127.0.0.1:6379> LLEN dialer
(integer) 3
127.0.0.1:6379>
127.0.0.1:6379>
[[email protected] go]# go run raspredelitel.go
Call Queue Balancer with Redis + FreeSWITCH
PONG Socket
Redis is Connected
Number of Rows in the Redis List for the service Automatic calling customers 0
Can you tell me what is the reason for the fact that after the first script writes lines to Radis, the second script for extracting data cannot take them, even though it even deletes the records.
Thanks

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
T
Tyranron, 2017-12-23
@sergey_fs

Well, you explicitly delete them (data). You have that in the code of the producer, that in the code of the consumer, there init()is a call in the function client.FlushDB().
I suspect you added this call to the producer to ensure the "purity of the experiment". And when they transferred the code to the consumer, they simply forgot to remove it. As a result, when your consumer starts up, it connects, cleans up the entire database, and then tries to read something from it, and, naturally, there is nothing to read.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question