在 Sinatra 中缓存响应的最佳方式?

发布于 2024-12-21 01:47:22 字数 174 浏览 2 评论 0原文

我正在使用我用 Sinatra 创建的 API 构建一个简单的应用程序,该 API 返回一些 JSON。这是相当多的 JSON,我的应用程序的 API 依赖于对其他 API 的数百个请求。

我大概可以将结果缓存 5 天左右,数据完全没有问题。我只是不是 100% 确定如何实现缓存。我该如何与 Sinatra 合作呢?

I'm building a simple app on the side using an API I made with Sinatra that returns some JSON. It's quite a bit of JSON, my app's API relies on a few hundred requests to other APIs.

I can probably cache the results for 5 days or so, no problem with the data at all. I'm just not 100% sure how to implement the caching. How would I go about doing that with Sinatra?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-12-28 01:47:22

就我个人而言,相对于 memcached,我更喜欢使用 redis 来处理此类事情。我有一个应用程序,我非常广泛地使用 redis,使用它的方式与您所描述的类似。如果我进行未缓存的调用,页面加载时间将超过 5 秒,而使用 redis,加载时间将降至 0.3 秒左右。您还可以设置过期时间,该时间可以很容易地更改。我会做这样的事情来从缓存中检索数据。

require 'redis'
get '/my_data/:id' do
  redis = Redis.new
  if redis[params[:id]]
    send_file redis[params[:id]], :type => 'application/json'
  end
end

然后当你想将数据保存到缓存时,也许是这样的:

require 'redis'
redis = Redis.new
<make API calls here and build your JSON>
redis[id] = json
redis.expire(id, 3600*24*5)

Personally, I prefer to use redis for this type of things over memcached. I have an app that I use redis in pretty extensively, using it in a similar way to what you described. If I make a call that is not cached, page load time is upwards of 5 seconds, with redis, the load time drops to around 0.3 seconds. You can set an expires time as well, which can be changed quite easily. I would do something like this to retrieve the data from the cache.

require 'redis'
get '/my_data/:id' do
  redis = Redis.new
  if redis[params[:id]]
    send_file redis[params[:id]], :type => 'application/json'
  end
end

Then when you wanted to save the data to the cache, perhaps something like this:

require 'redis'
redis = Redis.new
<make API calls here and build your JSON>
redis[id] = json
redis.expire(id, 3600*24*5)
一百个冬季 2024-12-28 01:47:22
get '/my_data/:id' do
  # security check for file-based caching
  raise "invalid id" if params[:id] =~ /[^a-z0-9]/i
  cache_file = File.join("cache",params[:id])
  if !File.exist?(cache_file) || (File.mtime(cache_file) < (Time.now - 3600*24*5))
    data = do_my_few_hundred_internal_requests(params[:id])
    File.open(cache_file,"w"){ |f| f << data }
  end
  send_file cache_file, :type => 'application/json'
end

不要忘记mkdir 缓存

或者,您可以使用 memcache-client,但它需要您在系统范围内安装 memcached

get '/my_data/:id' do
  # security check for file-based caching
  raise "invalid id" if params[:id] =~ /[^a-z0-9]/i
  cache_file = File.join("cache",params[:id])
  if !File.exist?(cache_file) || (File.mtime(cache_file) < (Time.now - 3600*24*5))
    data = do_my_few_hundred_internal_requests(params[:id])
    File.open(cache_file,"w"){ |f| f << data }
  end
  send_file cache_file, :type => 'application/json'
end

Don't forget to mkdir cache.

alternatively you could use memcache-client, but it will require you to install memcached system-wide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文