在 Sinatra 中缓存响应的最佳方式?
我正在使用我用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,相对于 memcached,我更喜欢使用 redis 来处理此类事情。我有一个应用程序,我非常广泛地使用 redis,使用它的方式与您所描述的类似。如果我进行未缓存的调用,页面加载时间将超过 5 秒,而使用 redis,加载时间将降至 0.3 秒左右。您还可以设置过期时间,该时间可以很容易地更改。我会做这样的事情来从缓存中检索数据。
然后当你想将数据保存到缓存时,也许是这样的:
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.
Then when you wanted to save the data to the cache, perhaps something like this:
不要忘记
mkdir 缓存
。或者,您可以使用
memcache-client
,但它需要您在系统范围内安装memcached
。Don't forget to
mkdir cache
.alternatively you could use
memcache-client
, but it will require you to installmemcached
system-wide.