在Golang(revel)中缓存数据库结果。

huangapple go评论80阅读模式
英文:

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)
}

huangapple
  • 本文由 发表于 2015年4月6日 21:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/29472690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定