memcached - Rails Action Caching for user specific records -
i rails newbie trying implement caching app. installed memcached , configured in development.rb follows:
config.action_controller.perform_caching = true config.cache_store = :mem_cache_store
i have controller productscontroller shows user specific products when logged in.
class productscontroller < applicationcontroller caches_action :index, :layout => false before_filter :require_user def index @user.products end end route index action is: /products
the problem when login
1) user first time, rails hits controller , caches products action.
2) logout , login user b, still logs me in user , shows products user , not user b. doesn't hit controller.
the key route, in memcached console see it's fetching based on same key.
20 views/localhost:3000/products 20 sending key views/localhost:3000/products
is action caching not should use? how cache , display user specific products?
thanks help.
the first problem before_filter require_user after action caching, won't run. fix that, use controller code:
class productscontroller < applicationcontroller before_filter :require_user caches_action :index, :layout => false def index @products = @user.products end end
second, action caching doing exact same thing page caching, after filters run, @user.products code won't run. there couple of ways fix this.
first, if want can cache action based on parameters passed page. example, if pass user_id parameter, cache based on parameter this:
caches_action :index, :layout => false, :cache_path => proc.new { |c| c.params[:user_id] }
second, if want cache query , not entire page, should remove action caching entirely , cache query, this:
def index @products = rails.cache.fetch("products/#{@user.id}"){ @user.products } end
this should on way having separate cache each user.
Comments
Post a Comment