Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Trong một chừng mực nào đó, vấn đề này được giải quyết bằng cách hỗ trợ giao dịch (mà chúng tôi mô tả trên trang 237). Tuy nhiên, vẫn sẽ có lần khi bạn cần phải làm mới một đối tượng mô hình. Ghi lại hoạt động này dễ dàng, chỉ cần gọi lại () | Caching Part One 321 Expiring Pages Creating cached pages is only one half of the equation. If the content initially used to create these pages changes the cached versions will become out-of-date and we ll need a way of expiring them. The trick is to code the application to notice when the data used to create a dynamic page has changed and then to remove the cached version. The next time a request comes through for that URL the cached page will be regenerated based on the new content. Expiring Pages Explicitly The low-level way to remove cached pages is with the expire_page and expire_action methods. These take the same parameters as url_for and expire the cached page that matches the generated URL. For example our content controller might have an action that allows us to create an article and another action that updates an existing article. When we create an article the list of articles on the public page will become obsolete so we call expire_page passing in the action name that displays the public page. When we update an existing article the public index page remains unchanged at least it does in our application but any cached version of this particular article should be deleted. Because this cache was created using caches_action we need to expire the page using expire_action passing in the action name and the article id. def create_article article Article.new params article if article.save expire_page action public_content else . end end def update_article article Article.new params article if article.save expire_action action premium_content id article else . end end The method that deletes an article does a bit more work it has to both invalidate the public index page and remove the specific article page. def delete_article Article.destroy params id expire_page action public_content expire_action action premium_content id params id end Report erratum Caching Part One 322 Expiring Pages Implicitly The expire_xxx methods work well but they also couple the .