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
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 )