简体   繁体   English

如何在 Go 中的特定时间间隔将数据存储到 redis

[英]How to store data to redis at specific Time interval in Go

I am trying to Set Data to Redis at (6 am, 12 pm, 6 pm and 12 am).我正在尝试在(上午 6 点、下午 12 点、下午 6 点和上午 12 点)将数据设置为 Redis。 But all I can do is setting an expiration time for data caching in redis.但我能做的就是在 redis 中设置数据缓存的过期时间。 Is there any way in Golang to do this? Golang有什么办法可以做到这一点吗?

Code:代码:

err := client.Set(key, data[]byte, 6 * time.Hour).Err()

Since you want to add data in redis at evry 6 hours.由于您想每 6 小时在 redis 中添加数据。 You should use cronjob for this.您应该为此使用 cronjob。

I have created a sample scenario which might help you as follows:我创建了一个示例场景,可能会对您有所帮助:

For better understanding you can refer gocron package.为了更好地理解,您可以参考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)
        }
    
     })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM