今天我尝试使用ActiveModelSerializers设置缓存。我遇到了很多问题(我的连接时间已经超时)。所以我试图恢复我的所有更改,包括删除我正在处理的分支。
然而,自从我尝试设置缓存以来,我正在使用的模型表现出奇怪的行为。看起来它仍在加载缓存元素。在尝试访问该模型的端点时,这会导致巨大的性能损失。
header-transparent
Started GET "/api/v1/entities" for ::1 at 2016-11-17 16:50:44 -0700
ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by Api::V1::EntitiesController#index as JSON
Entity Load (0.4ms) SELECT "entities".* FROM "entities" ORDER BY "entities"."name" ASC
Group Load (0.3ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" IN (2, 3, 1) ORDER BY "groups"."name" ASC
Platform Load (0.4ms) SELECT "platforms".* FROM "platforms" WHERE "platforms"."id" IN (3, 8) ORDER BY "platforms"."name" ASC
HardwareType Load (19.3ms) SELECT "hardware_types".* FROM "hardware_types" WHERE "hardware_types"."id" IN (1, 3, 19, 8)
EntityType Load (0.2ms) SELECT "entity_types".* FROM "entity_types" WHERE "entity_types"."id" IN (1)
EntityOperationalState Load (0.4ms) SELECT "entity_operational_states".* FROM "entity_operational_states" WHERE "entity_operational_states"."entity_id" IN (1, 2, 3, 4)
OperationalState Load (0.2ms) SELECT "operational_states".* FROM "operational_states" WHERE "operational_states"."id" IN (1) ORDER BY "operational_states"."name" ASC
EntityLifecycleState Load (0.4ms) SELECT "entity_lifecycle_states".* FROM "entity_lifecycle_states" WHERE "entity_lifecycle_states"."entity_id" IN (1, 2, 3, 4)
LifecycleState Load (0.3ms) SELECT "lifecycle_states".* FROM "lifecycle_states" WHERE "lifecycle_states"."id" IN (2) ORDER BY "lifecycle_states"."name" ASC
IpAddress Load (0.3ms) SELECT "ip_addresses".* FROM "ip_addresses" WHERE "ip_addresses"."entity_id" IN (1, 2, 3, 4)
IpType Load (0.2ms) SELECT "ip_types".* FROM "ip_types" WHERE "ip_types"."id" IN (1)
IpUse Load (0.4ms) SELECT "ip_uses".* FROM "ip_uses" WHERE "ip_uses"."id" IN (1, 2)
[active_model_serializers] Entity Load (0.3ms) SELECT "entities".* FROM "entities" WHERE "entities"."id" = ? ORDER BY "entities"."name" ASC LIMIT 1 [["id", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT "entities".* FROM "entities" WHERE "entities"."id" = ? ORDER BY "entities"."name" ASC LIMIT 1 [["id", 1]]
[active_model_serializers] Entity Load (0.2ms) SELECT "entities".* FROM "entities" WHERE "entities"."id" = ? ORDER BY "entities"."name" ASC LIMIT 1 [["id", 2]]
[active_model_serializers] CACHE (0.0ms) SELECT "entities".* FROM "entities" WHERE "entities"."id" = ? ORDER BY "entities"."name" ASC LIMIT 1 [["id", 2]]
[active_model_serializers] Entity Load (0.2ms) SELECT "entities".* FROM "entities" WHERE "entities"."id" = ? ORDER BY "entities"."name" ASC LIMIT 1 [["id", 3]]
[active_model_serializers] CACHE (0.0ms) SELECT "entities".* FROM "entities" WHERE "entities"."id" = ? ORDER BY "entities"."name" ASC LIMIT 1 [["id", 3]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Json (71.13ms)
Completed 200 OK in 617ms (Views: 474.3ms | ActiveRecord: 27.8ms)
加载和CACHE是让我相信序列化程序仍在尝试从缓存加载的新内容。
我的其他模型,我没有用一个[active_model_serializers]
语句来加载,这是我预期的行为。
[active_model_serializers]
正如您所见,那些额外的数据库访问会减慢我的加载时间。这些模型现在都有大约3个对象,但是当我测试更多的记录时,这就成了一个很大的问题。
如何强制ActiveModelSerializer忽略缓存的项目?
更好的选择是找出缓存这些片段的位置,以便我可以清除它们。我试过Rails.cache.clear,但这没有效果。我也试过,重新启动计算机,还关闭我的Redis实例。
答案 0 :(得分:0)
没关系。显然这与我正在进行的缓存工作无关。
我在has_many关系中有一个实体名称的循环引用