skip to Main Content

I am trying to get data from MySQL database and store it every 6 hours later on redis. I am totally new in redis. I want to get data from MySQL DB using Select * from table_name. Then store those data into redis. Is there any example in Golang Beego framework of storing data and getting data from redis?

3

Answers


    1. you could user go-redis and redi-go as tool to connect redis,and then choose the data type you need.

    example: go-redis:

    https://github.com/go-redis/redis

    var ctx = context.Background()
    
    func ExampleClient() {
        //01 connect
        rdb := redis.NewClient(&redis.Options{
            Addr:     "localhost:6379",
            Password: "", // no password set
            DB:       0,  // use default DB
        })
        err := rdb.Set(ctx, "key", "value", 0).Err()
        if err != nil {
            panic(err)
        }
        val, err := rdb.Get(ctx, "key").Result()
        if err != nil {
            panic(err)
        }
        fmt.Println("key", val)
    }
    
    1. struct to redis

    type User struct {
    UserID uint32
    UserName string
    }
    var ctx = context.Background()

    // Set set to redis as string
    func Set() {
        var u User
        marshal, err := json.Marshal(&u)
        if err != nil {
            log.Println(err)
        }
        rdb.Set(ctx,"user",string(marshal),-1)
        //-1 means no expiration time
    }
    
    // Get get from redis
    func Get() {
        var u User
        bytes, err := rdb.Get(ctx, "user").Bytes()
        if err != nil {
            log.Println(err)
        }
        err = json.Unmarshal(bytes, &u)
        if err != nil {
            log.Println(err)
        }
    }
    
    Login or Signup to reply.
  1. You should use redis along with cron-job. Since, you want to store the data at every 6 hours in redis.

    I have created a sample example for your scenario as follows :

    func SetData(c *RedisClient, key string, value interface{}){
     
            //-1 means no expiration time
            err := c.Set(ctx,key,value,-1).Err()
       
           if err != nil{
             panic(err)
           }
    
    }
    
    func GetData() {
    
        //fetch your table data from database
        //take instance of redis client
        //call SetData()
            //var user_val model.User{}
        //assuming you have fetched data in user_val
    
        SetData(redis_client_instance, "user", user_val)
    }
    

    Now, call the GetData() inside cron-job which start mid-night at 12 AM and add the data at every 6 in the redis.

     //pick the start time inside NewScheduler
     //if you want you can load your zone time.
        s := gocron.NewScheduler(time.UTC)
        
        s.Every(6).Hours().Do(GetData)
    
    Login or Signup to reply.
  2. I am not sure you are asking for codes by using Beego. Or you just need the guidance for your problem. There are several ways to do it, and they all have their own drawbacks.

    1. Use ticket or cronjob to load recent 6 hours data. You can not read the most rent update or insert from cache before the next ticker run.

    2. Double write both in MYSQL and REDIS. The REDIS cache can be warmed up for loading recent 6 hours data for once. When you write MYSQL successfully but REDIS failed, you may have the inconsistent data between REDIS and MYSQL.

    3. Subscribe the bin log of MYSQL and replay all bin logs on REDIS. You may find some open source project to do it on Github. It an complicate solution for this problem, but it is really common in IT companies.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search