英文:
Caching database results in Golang (revel)
问题
我目前正在使用Revel Web框架编写一个Go的Web应用程序。我已经到了想要从数据库中提供结果的阶段,但是要提供的行数相当多(约5000-10000行)。这些信息每3分钟才会更改一次,所以也许实现某种形式的缓存是个好主意。
Revel框架提供了一个缓存解决方案,但是我不知道这样的东西是如何工作的,也不确定它是否是解决我的问题的最佳方案。另一个解决方案可能是创建一个全局数组来存储结果,并定期获取一个切片(如果有很多用户,这种方法是否更好?)。
你们能帮助我吗?我会非常感激的。
英文:
I am currently coding a web application in Go with the help of the revel web framework. I've come to the point where I want to serve results from a database, however there's quite an amount of rows to serve (~5000-10000). The information only changes every 3 minutes, so perhaps it's a good idea to implement some form of caching.
The Revel framework offers a caching solution, however I have no idea how such a thing would work and if it's the best solution to my problem. Another solution could be to make a global array with the results and grab a slice once in a while (Would this work better if there are a lot of users? ).
Could you guys help me out? I'd really appreciate it.
答案1
得分: 1
在Revel中添加以下内容:
cache.memcached = true
cache.hosts="127.0.0.1:11211"
确保你已经安装了Memcached。然后在你的代码中导入github.com/revel/revel/cache
,你可以使用以下代码:
var results []string
if err := cache.Get("res", &results); err == nil {
// 使用 results
} else {
// 进行数据库查询
cache.Set("results", results, 3*time.Minute)
}
英文:
In revel add
cache.memcached = true
cache.hosts="127.0.0.1:11211
To your conf/app.conf and make sure you have memcached installed. Then import github.com/revel/revel/cache
and in your code you can use
var results []string
if err := cache.Get("res", &results); err == nil {
// use results
} else {
// do db query
cache.Set("results", results, 3*time.Minute)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论