0

I am trying to Set Data to Redis at (6 am, 12 pm, 6 pm and 12 am). But all I can do is setting an expiration time for data caching in redis. Is there any way in Golang to do this?

Code:

err := client.Set(key, data[]byte, 6 * time.Hour).Err()
  • Looks like you're trying to set some data every 6 hours starting at 6am. Why not use [cron.jobs](https://github.com/robfig/cron) for this? – christianfoleide Dec 06 '21 at 13:06

1 Answers1

1

Since you want to add data in redis at evry 6 hours. You should use cronjob for this.

I have created a sample scenario which might help you as follows:

For better understanding you can refer gocron package.

s := gocron.NewScheduler(time.UTC)
    
    s.Every(6).Hours().Do(func(){  //you can change it
    
      err := client.Set(ctx, "key", "value", 0).Err()  //your logic
        if err != nil {
            panic(err)
        }
    
     })
Ashutosh Singh
  • 721
  • 3
  • 6