Rails Caching DB Queries and Best Practices

后端 未结 4 1261
小蘑菇
小蘑菇 2021-01-31 04:23

The DB load on my site is getting really high so it is time for me to cache common queries that are being called 1000s of times an hour where the results are not changing. So f

4条回答
  •  伪装坚强ぢ
    2021-01-31 04:45

    With respect to the caching, a couple of minor points:

    It's worth using slash for separation of object type and id, which is rails convention. Even better, ActiveRecord models provide the cacke_key instance method which will provide a unique identifier of table name and id, "cities/13" etc.

    One minor correction to your after_save filter. Since you have the data on hand, you might as well write it back to the cache as opposed to delete it. That's saving you a single trip to the database ;)

    def after_save
      Rails.cache.write(cache_key,self)
    end
    

    As to the root of the question, if you're continuously pulling @user.city.name, there are two real choices:

    • Denormalize the user's city name to the user row. @user.city_name (keep the city_id foreign key). This value should be written to at save time.

    -or-

    • Implement your User.fetch method to eager load the city. Only do this if the contents of the city row never change (i.e. name etc.), otherwise you can potentially open up a can of worms with respect to cache invalidation.

    Personal opinion: Implement basic id based fetch methods (or use a plugin) to integrate with memcached, and denormalize the city name to the user's row.

    I'm personally not a huge fan of cached model style plugins, I've never seen one that's saved a significant amount of development time that I haven't grown out of in a hurry.

    If you're getting way too many database queries it's definitely worth checking out eager loading (through :include) if you haven't already. That should be the first step for reducing the quantity of database queries.

提交回复
热议问题