0

I have been googling for a long time and haven't find a solution yet. I am using Rails 3.2 and the Dalli gem with memcached 1.4.14.

I would like to use Dalli/Memcached as the session store but I dont know how to do that. Now, Dalli cache works like a classic cache but not the session cache. I created a new rails app, added Dalli to the Gemfile :

gem 'dalli'

In development and production environments I set Dalli as the cache store:

config.cache_store = :dalli_store

This is the content of initializers/session_store.rb:

require 'action_dispatch/middleware/session/dalli_store'
Rails.application.config.session_store :dalli_store, :memcache_server => ['127.0.0.1'], :namespace => 'sessions', :key => '_session', :expire_after => 30.minutes

I create a Myooo model in which I added to methods to list and create instances:

def self.index_c
a = Rails.cache.read(:myooo_index)
if a.nil?
    puts '--->>> load again'
    a = Rails.cache.write(:myooo_index, Myooo.all, expires_in: 5.seconds)
else
    puts '--->>> using cache'
end
return Rails.cache.read(:myooo_index)
end

def self.create_c(myooo_object)
a = Rails.cache.read(:myooo_create)
if a.nil?
    a = Rails.cache.write(:myooo_create, myooo_object, expires_in: 10.minutes)          
end
return Rails.cache.read(:myooo_create)
end

In the controller, I just try to reach the cache. In the index action:

@myooos = Myooo.index_c

and in the create action:

@myooo = Myooo.new(params[:myooo])
Myooo.create_c(@myooo)

I know it's a strange script but i need to understand how I'm supposed to deal with the session cache and use it in a real project. I spent a lot of time in server outline console, made many puts trying to understand what's going on. Cache seems to act like a classic cache, not a session cache.

I would be glad to be helped (ps: happy new year) :)

Jef
  • 5,424
  • 26
  • 28
smonty
  • 73
  • 4
  • 7
  • By session store you should understand the store used to persist the data corresponding to the users HTTP sessions. Except in your initializer, nothing in the code you shown relates to the HTTP session. Could you clarify what you are trying to achieve ? – Jef Jan 01 '13 at 16:20
  • 1
    Simply use `session` to store data into the user's session (e.g. `session[:user_profile]=@profile`). If you need to cache large amounts of data you'd rather identify what your actual performance issues are before choosing a caching strategy. Rails come with [several builtin options](http://guides.rubyonrails.org/caching_with_rails.html) for caching. In your case partial caching coupled with [key-based](http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works#extended) expiration strategy (Memcached LRU eviction) could be worth considering. – Jef Jan 01 '13 at 17:33
  • thanks for links and help. i was thinking to solve it as you are giving me advice session[:sess] = @asd but i hope dalli or any other gem will solve this problem and i won't manage session and cache. – smonty Jan 01 '13 at 19:26
  • no, i try to code some lines and i have new view to this problem: 1) cache don't know nothing about session, so i have to make may lines to manage it... i can make session nil, but when i ask cache, i get bad data which dont correspondent... 2) i can't work with session in model, so my code is spred into controller (where iam managing session) and model (where iam managing cache). – smonty Jan 02 '13 at 12:18
  • so i need mechanism to handle in. i have read dalli in github and there is something about [session store](https://github.com/mperham/dalli#usage-with-rails-3x) but there is no doc to say how to do it. thanks for help – smonty Jan 02 '13 at 12:19

1 Answers1

0

(i wrote longer text cca 500 chars) yes you are right, i am talking about this. i am building big app, about 30 db tables where all is centralized with user table (for me it's big, i have never work in biggiest project). when user login, i would like to cache data which user claim. there are lot of queries to database so i would like to cache them. is there other way to solve my problem and don't use cached data stored in session?

the example with create is only about testing... (not real test, but test for me, if it is working right) in opera i fill form and call create method. i expect that data will be store only for user with opera. in view, i make something like this:

 <% a = Rails.cache.read(:myooo_create) %>
 <% if !a.nil? %>
 <%= a.myi %><br/>
 <%= a.mys %>
 <% end %>

but when i move to firefox, data was there... so i guess, this data was not save in session cache but to "general cache". (of course, this view code is soo bad! but for dalli study, it is good ;) )

smonty
  • 73
  • 4
  • 7