gpt4 book ai didi

ruby-on-rails - 事件模型序列化程序 10 缓存。它似乎不起作用。为什么?

转载 作者:行者123 更新时间:2023-12-03 16:04:23 28 4
gpt4 key购买 nike

我的玩具应用有几个模型,rental_units 和 users。你可以找到 repo here

我正在运行 Rails 5 和 AMS 10。

active_model_serializers (0.10.0.rc4)
...
rails (5.0.0.beta3)
actioncable (= 5.0.0.beta3)
actionmailer (= 5.0.0.beta3)
actionpack (= 5.0.0.beta3)
...

我有一个如下所示的 RentalUnitSerializer:
class RentalUnitSerializer < ActiveModel::Serializer
cache key: 'rental_unit', expires_in: 3.hours
attributes :id, :rooms, :bathrooms, :price, :price_cents

belongs_to :user
end

这是我的 UserSerializer:
class UserSerializer < ActiveModel::Serializer
cache key: 'user'
attributes :id, :name, :email
has_many :rental_units

def name
names = object.name.split(" ")
"#{names[0].first}. #{names[1]}"
end
end

这是我的 Gemfile 的一部分:
    source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma'
gem 'active_model_serializers', '~> 0.10.0.rc1'
gem "dalli"
gem "memcachier"

这是我的 config/environments/development.rb 文件:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Do not eager load code on boot.
config.eager_load = false

# Show full error reports.
config.consider_all_requests_local = true

# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true

config.action_mailer.perform_caching = false

config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = true

config.action_mailer.perform_caching = false

config.cache_store = :memory_store
end

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load


# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

这是奇怪的行为

当我访问 http://localhost:3000/users ,这是我没有缓存迹象的日志:
Started GET "/users" for ::1 at 2016-03-04 15:18:12 -0500
Processing by UsersController#index as HTML
User Load (0.1ms) SELECT "users".* FROM "users"
[active_model_serializers] RentalUnit Load (0.2ms) SELECT "rental_units".* FROM "rental_units" WHERE "rental_units"."user_id" = ? [["user_id", 1]]
[active_model_serializers] RentalUnit Load (0.1ms) SELECT "rental_units".* FROM "rental_units" WHERE "rental_units"."user_id" = ? [["user_id", 2]]
[active_model_serializers] RentalUnit Load (0.1ms) SELECT "rental_units".* FROM "rental_units" WHERE "rental_units"."user_id" = ? [["user_id", 3]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModel::Serializer::Adapter::JsonApi (11.0ms)
Completed 200 OK in 13ms (Views: 12.5ms | ActiveRecord: 0.5ms)

当我访问 http://localhost:3000/rental_units ,有一些缓存?
Started GET "/rental_units" for ::1 at 2016-03-04 15:18:37 -0500
Processing by RentalUnitsController#index as HTML
RentalUnit Load (0.4ms) SELECT "rental_units".* FROM "rental_units"
[active_model_serializers] User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
[active_model_serializers] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModel::Serializer::Adapter::JsonApi (16.1ms)
Completed 200 OK in 21ms (Views: 19.1ms | ActiveRecord: 1.4ms)

到底是怎么回事?看起来在后面的日志中,用户正在被缓存?我相信由于 3 个不同的出租单元属于用户 1,AMS 有一些默认缓存,其中相同的 SQL 查询只是命中某种缓存。因此,对 User1 的第一个查询会命中服务器的数据库,但对 User1 的后续查询不会。这个缓存在哪里?是在服务器上吗?或者一些网络服务器?

但我认为我的 Serializer 中的缓存策略根本不起作用。为什么?

最佳答案

为了帮助调试,请尝试设置 logger为您的缓存。 Rails 缓存支持设置记录器,但 MemoryStore默认情况下不设置它。

在初始化程序中,尝试以下操作:

# config/initializers/cache.rb
Rails.cache.logger = Logger.new(STDOUT)
# or Rails.cache.logger = Rails.logger

重新启动 Rails 服务器,您应该会看到缓存命中/未命中的日志记录。我怀疑它正在工作,但可能不是您期望的方式。

既然你问了,实际上有几层缓存你的问题涉及:片段(或 View )缓存和查询缓存。

您已将缓存启用为 :memory_store这将创建一个 ActiveSupport::Cache::MemoryStore 的实例在您的应用程序和 Rails 控制台中访问为 Rails.cache .

您还设置了 cache序列化程序的选项。来自 ActiveModelSerializer guides对于您的版本,您似乎已正确设置。到现在为止还挺好。

通常,在 Rails Controller 中,除非您渲染 View ,否则不会调用 Rails 缓存。在您的情况下,您正在呈现一个 json View (通过 AMS);它与 HTML 模板的工作方式相同。

第一次发出这个请求时,缓存是冷的,进行数据库查询,Rails 使用缓存键(或多个键)向缓存请求 JSON 字符串的表示。由于这组资源缓存为空,所以必须生成JSON字符串,然后缓存在 Rails.cache ,然后返回响应。

下次发出此请求时(在到期窗口内),会进行 DB 查询——Rails 需要知道哪些资源要请求缓存对吗? - 这一次,缓存命中,字符串从缓存中返回。不需要生成新的 JSON。请注意,这仍然会导致请求原始资源的数据库命中。这些请求是 AMS 为您的 JSON 查找缓存键所必需的。

您还会看到 CACHE select ...日志中的查询也是如此。正如在另一个答案中提到的,这可能表明您一遍又一遍地进行相同的数据库查询(通常表示 N+1 query ),因此建议“急切加载”这些关系。您的 RentalUnit属于 :user ,因此对于每个出租单元,都会为每个用户发出一个单独的请求 - 您可以通过预先加载在一个查询中获取所有这些用户。

Rails 为 cache the results of SQL queries 提供了一些功能.这与我们一直在讨论的为 AMS 启用的 View 缓存是分开的。

关于ruby-on-rails - 事件模型序列化程序 10 缓存。它似乎不起作用。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805032/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com