简体   繁体   English

Rails活动记录在内存中的缓存

[英]rails active record caching in memory

I have a model which has through relationships to another model through two foreign key relationships. 我有一个模型,该模型通过两个外键关系与另一个模型具有联系。 I do a lot of lookups on those tables (which are 3-4K rows) while importing a lot of data, and I'm trying to eliminate spurious repeated database lookups. 在导入大量数据的同时,我对那些表(3-4K行)进行了很多查找,并且我试图消除虚假的重复数据库查找。 (Ideally, my database would be doing async writes/INSERTs only) (理想情况下,我的数据库只会执行异步写入/插入操作)

I have played with doing my own caching by ID, and recently switched to using Rails.cache (with MemoryStore for now. I have no need to sync against other instances, and I'm ram rich on the import machine). 我玩过按ID进行自己的缓存,最近切换到使用Rails.cache (现在使用MemoryStore。现在无需与其他实例同步,并且在导入计算机上非常丰富)。 However, I find that I am getting multiple copies of the same associated records, and I'd like to get rid of this. 但是,我发现我正在获得相同关联记录的多个副本,并且我想摆脱它。

For instance: 例如:

irb> p = Phone.includes([:site => :client, :btn => :client]).first. 

irb> p.site.client.object_id => 67190640 

irb> p.btn.client.object_id => 67170780

Ideally, I'd like these to point to the same object in memory. 理想情况下,我希望它们指向内存中的同一对象。 Rails.cache would serialize things in/out, which really just makes this worse, but I was surprised by this. Rails.cache会序列化事物的进/出,这只会使情况变得更糟,但是我对此感到惊讶。 Could I override find_by_id() or some such in a way that the association proxies would make use of my cache? 我可以用关联代理将利用我的缓存的方式覆盖find_by_id()或类似的东西吗?

Maybe there is another caching module that I'm missing? 也许我缺少另一个缓存模块?

(please note that there is no web front end involved in this process. It's all models and ORM) (请注意,此过程不涉及Web前端。所有模型和ORM)

Try using IdentityCache (see https://github.com/Shopify/identity_cache ). 尝试使用IdentityCache (请参阅https://github.com/Shopify/identity_cache )。 We currently have a similar issue. 我们目前有一个类似的问题。 We're using JRuby because it's fast but mallocs are expensive in our target environment... making cacheing these record instances all the more necessary. 我们之所以使用JRuby是因为它速度很快,但在目标环境中malloc却很昂贵……因此更加需要缓存这些记录实例。

There used to be an IdentityMap within ActiveRecord, but it was removed due to issues with unexpected behaviour around associations. ActiveRecord内曾经有一个IdentityMap ,但是由于关联周围的意外行为问题而被删除。

Just noticed you asked this in August, did you find a good solution? 刚刚注意到您在八月份问过这个问题,您找到了一个好的解决方案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM