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
example: go-redis:
type User struct {
UserID uint32
UserName string
}
var ctx = context.Background()
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 :
Now, call the
GetData()
inside cron-job which start mid-night at 12 AM and add the data at every 6 in the redis.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.
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.
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.
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.