At some point in my Rails application, I retrieve a large number of ActiveRecord objects from the cache. However, it’s possible that some of these records have been deleted from the database itself since they were stored in the cache, so I loop the records and check each one to see if it exists. This takes quite a lot of time. Is there a more efficient way to do this?
Share
Is there a reason why you are not deleting the records from the cache when they are deleted from the database?
If you are going to be storing these records in the cache and need them in sync with the db, then when you remove them from the db make sure to remove their existence from the cache as well, therefore saving yourself the expensive query of having to check for redundant data later.