# Lazy-loaded sessions with memcache under 2.3.4?

**URL:** <https://www.ruby-forum.com/t/lazy-loaded-sessions-with-memcache-under-2-3-4/178314>\
**Category:** Rails\
**Created:** [November 2, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/lazy-loaded-sessions-with-memcache-under-2-3-4/178314 "2009-11-02T18:18:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wisq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@Wisq](https://www.ruby-forum.com/u/Wisq)\
**Post date:** [November 2, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/lazy-loaded-sessions-with-memcache-under-2-3-4/178314/1 "2009-11-02T18:18:14Z")

</div>

I’ve got a Rails 2.2.3 that I recently upgraded to 2.3.4. I made a  
new Rails tree and copied most of the stuff under “config” over (from  
the new tree to my existing tree) in order to get things to a clean  
2.3.x state. Rails is frozen-in under “vendor” and “script/server”  
verifies I’m running 2.3.4, so there’s no old code lying around.

I decided to try out memcache-based sessions on my Rails app,  
according to this URL:

> **[Memcache Sessions in Rails](https://awesomerails.wordpress.com/2008/06/26/memcache-sessions-in-rails/)**
>
> UPDATE FOR RAILS 3: Does rails scale? No! But memcached does! Ok ok, you aren’t probably going to need memcache for caching huge queries yet.  But it is useful for storing your user’s s…

I’ve got memcache-client installed, and my config/initializers/  
session\_store.rb looks like this:

require ‘memcache’  
CACHE = MemCache.new(:namespace =\> ‘myprojectname’)  
CACHE.servers = ‘localhost:11211’

ActionController::Base.session = {  
:session\_key =\> ‘\_myprojectname\_session’,  
:cache =\> CACHE,  
:expires =\> 86400,  
:secret =\> ‘128-random-characters’  
}  
ActionController::Base.session\_store = :mem\_cache\_store

Now, sessions work fine. The only trouble is, my application doesn’t  
yet touch the session in any conceivable way – and yet, according to  
my memcached logs, I’m getting sessions fetched and stored with every  
single request. This runs contrary to the claims in the 2.3.x  
changelogs, which say that sessions are lazy-loaded and only loaded/  
stored when accessed now.

I’ve tried multiple environments in case it was specific to one of  
them: WEBrick, Thin, FastCGI. All the same behaviour.

Since I only plan to use sessions for a small part of the application,  
this is rather sub-optimal, not least because anyone who disables  
cookies (including myself, typically) is going to be spamming my  
session store with a new session every request.

I realise I can just disable sessions for controllers I don’t want,  
but while there are only a few parts of the app that will write to the  
session, there are several that will want to read it (if available).  
Plus, disabling sessions per controller is deprecated because it’s  
supposedly unnecessary these days (due to lazy loading), isn’t it?  
That’s the main reason I bothered to upgrade in the first place.

Am I missing something here, or is lazy loading just not implemented  
for memcached sessions?
