如何缓存Rails API操作



缓存操作的最佳和正确方法是什么

  • 我是否被迫使用ActionController::Base
  • 是否有其他方法(保持ActionController::API存在(
  • 我必须把缓存推到模型层吗

我看到Rails 6(可能是以前的版本(不再支持开箱即用的操作缓存。缓存被提取到一个gem:actionpack-action_cachinghttps://github.com/rails/actionpack-action_caching.我安装了它,但它似乎不适用于ActionController::API,它只能用于ActionController::Base

class ApplicationController < ActionController::API

必须更改为

class ApplicationController < ActionController::Base

然后,也只有这样,我才能缓存这样的操作:

class CategoryController < ApplicationController
caches_action :index
def index
@root_categories = Category.roots
end
end

提前感谢

gem通过在ActionController::Base中包含ActionController::Caching将这些方法添加到控制器中(请参阅此处(。要将此行为添加到不从ActionController::Base扩展的控制器,只需包含ActionController::Caching即可。

例如

class ApplicationController < ActionController::API
include ActionController::Caching
end

最新更新