rs-pro / mongoid-elasticsearch

DEPRECATED - Consider using SearchKick https://github.com/ankane/searchkick
MIT License
61 stars 23 forks source link

Cache search question #21

Closed avra911 closed 9 years ago

avra911 commented 9 years ago

Hi,

I am doing a search and I am using kaminari as pagination

def self.get_items(query_term, page, per_page)
    Rails.cache.fetch("#{query_term}/#{page}/#{per_page}", expires_in: 1.month) do
        ...
        @items = Model.es.search({body:@body}, {page: page, per_page: per_page})

and view:

@items.each do |item|
    ...
= paginate @items, :window => 1, :outer_window => 1

I was wonder how I can cache the @items, because just doing the same way as with standard model cache throws me an error (no _dump_data is defined for class Proc):

Should I move the cache directive to view instead of model search function definition?

Thanks in advanced, R

avra911 commented 9 years ago

It appears that doing cache in view directly works:

- cache("#{params['q']}/#{params['page']}") do
    - @items.each do |item|

Any suggestion on best practice here? I just started ruby 3 weeks ago :)

Maybe I can cache the entire page (controller action) based on the params so it will work with pagination and search terms?

glebtv commented 9 years ago

Probably you should cache results

Model.es.search({body:@body}, {page: page, per_page: per_page}).results.to_a