Railing3 caching: expire fragment with extension

How to expire a fragment in a cache with a file extension (.json) from another controller?

I am using Rails 3.2.8.

I have two controllers for the same models: / admin / books_controller and / api / books _controller. An administrator controller is used to create / update / delete books and requires increased access. Api / books_controller is used to search / view book information. I tried to cache api / books_controller

class Api::BooksController < ApiController caches_action :show def show @book = Book.find(params[:id]) end end 

This works great. The first request is slow and the second is fast

 Rendered api/books/show.json.rabl (119.7ms) Write fragment views/localhost:3000/api/books/4.json (1.7ms) Completed 200 OK in 568ms (Views: 124.2ms | ActiveRecord: 9.5ms) Read fragment views/localhost:3000/api/books/4.json (0.2ms) Completed 200 OK in 16ms (ActiveRecord: 2.6ms) 

Now I need to end the fragments when updating or destroying the Book. I have it:

 class Admin::BooksController < AdminController def update @book.attributes = params[:book] if (@book.save) expire_action(:controller => 'api/books', :action => 'show', :id => @book.id, :format => 'json') end end end 

However, this does not allow the same fragment (the request now is an HTML request to another controller):

 Started PUT "/admin/books/4" for 127.0.0.1 at 2012-11-08 12:27:11 -0600 Processing by Admin::BooksController#update as HTML ... Expire fragment views/localhost:3000/api/books/4 (0.1ms) 

My API controller with a JSON request reads and writes this fragment:

 views/localhost:3000/api/books/4.json 

My admin is trying to complete this snippet:

 views/localhost:3000/api/books/4 

How do I stop viewing / localhost: 3000 / api / books / 4.json from another controller with a different request format?

This question: rails 3 caching: expire action for named route indicates that I can use a regular expression to expire several fragments, but this requires keys in an enumerated cache ( http://api.rubyonrails.org/classes/ActionController/Caching/ Fragments.html # method-i-expire_fragment )

+4
source share
1 answer

It seems like the problem was setting default options for routing. In the API controller, params [: format] was "json", and in the administrator controller it was zero.

When I added this to my Admin controller:

 params[:format] = 'json' expire_action(:controller => 'api/books', :action => 'show', :id => @book.id, :format => 'json') 

Then the fragments of both controllers match.

This allowed me to return to my routes:

In my routes, I had this:

 namespace :api, :defaults => {:format => 'json'} do resources :books, :only => [:show] end 

This resulted in params [: format] being installed in my api namespace. I had nothing in my admin namespace, and apparently the actionpack / ActionCachePath saw these two differently. Removing the default settings in routes resolves the issue.

0
source

Source: https://habr.com/ru/post/1444899/


All Articles