My response time is about 3 second, so I want to decrease the response time. The first thing that I think is returning from cache but I couldn't find any framework or explanation about that. How can I achieve it or is there any alternative solution?
Should I use this framework: https://github.com/mediocregopher/radix.v2
I am using postgresql
for db and in for loop I call this code:
err := GetDB().Table("table_name").Find(&words).Error
Your question is too broad but based on the discussion above, I believe what you're trying to do is to load some sort of static data from a database that will be used in your program, and it does not need to be refreshed. In that case, it makes sense to load the data when you connect the database once and keep it in a global variable under a package. For instance:
package model
var SomeData []SomeType
package main
func main() {
// Load data from the db
model.SomeData = loadedData
// Start the program...
}
This assumes model.SomeData
will not change during the program. If you need to, for instance, reload it periodically, a better structure for the program is:
package model
var someData []SomeType
var someDataLock sync.RWMutex
func GetData() []SomeType {
someDataLock.RLock()
defer someDataLock.RUnlock()
return someData
}
func SetData(in []SomeType) {
someDataLock.Lock()
defer someDataLock.Unlock()
someData=in
}
Then you use model.SetData
to set the data, and model.GetData
to get the data in a thread-safe manner.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.