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