Ruby on Rails Clean the cache
Learn how to clear your Ruby on Rails cache with an easy example.
Cleaning the Rails Cache
Ruby on Rails has several different types of caches that can be cleared, depending on the situation. It is important to understand the different types of caches and to know which one to clear when the need arises.
Fragment Caches
Fragment caches are used to cache an individual part of a web page. This can be used to cache complex and expensive parts of the page so that the page can be rendered quickly. Fragment caches are cleared when the data in the database changes. To clear the fragment caches, you can use the Rails.cache.clear
method.
Rails.cache.clear
Page Caches
Page caches are used to cache an entire web page. This can be used to improve the performance of a website by significantly reducing the amount of data that needs to be sent over the network. Page caches are cleared when the data in the database changes. To clear the page caches, you can use the Rails.page_cache.clear
method.
Rails.page_cache.clear
Asset Caches
Asset caches are used to store static assets such as images, JavaScript files, and CSS files. This can be used to reduce the amount of data that needs to be sent over the network. Asset caches are cleared when the files contained in the cache are modified. To clear the asset caches, you can use the Rails.asset_cache.clear
method.
Rails.asset_cache.clear
To clear all of the caches in one go, you can use the Rails.clear_all_caches
method. This will clear all of the fragment, page, and asset caches.
Rails.clear_all_caches
It is important to understand the different types of caches and when to use them. Knowing when and how to clear the caches is important for keeping your application running smoothly.