Question

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

No correct solution

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top