Caching models with associations in AuthenticatedSystem

35 views
Skip to first unread message

fightinjoe

unread,
Feb 6, 2007, 2:31:05 AM2/6/07
to acts_as_cached
In my simple app, I've got two models: users and groups. There is a
habtm relationship between the two.

I've setup caching for both. The setup includes the associations.
For example:

class User < ActiveRecord::Base
has_and_belongs_to_many :groups
acts_as_cached :include => [ :groups ]
...
end

I'm using AuthenticatedSystem for logging in users and I want to
replace the User.find calls with User.get_cache.

When I do this, I get the following error:

ArgumentError in GeneralController#index: undefined class/module Group

I believe this error is called because Marshal doesn't know about the
Group class. If I reference Group prior to the cache call, then
everything works fine. For example:

Group and User.get_cache( session[:user_id] )

Before I start trying to figure out Rails's auto-include hooks, does
anyone have any insight as to a better way to deal with this issue?

Cheers,

Aaron Wheeler

Chris Wanstrath

unread,
Feb 6, 2007, 2:54:08 AM2/6/07
to acts_as...@googlegroups.com
On 2/5/07, fightinjoe <fight...@gmail.com> wrote:

> When I do this, I get the following error:
>
> ArgumentError in GeneralController#index: undefined class/module Group
>

> Before I start trying to figure out Rails's auto-include hooks, does
> anyone have any insight as to a better way to deal with this issue?

This is interesting because acts_as_cached has code to trigger Rails'
autoloading in the situation you describe. Specifically:

http://require.errtheblog.com/plugins/browser/cache_fu/lib/acts_as_cached/cache_methods.rb

The "autoload_missing_constants" method (yes I know this is cache_fu
but the principle is the same, just cleaner in this version) should
load Group if it doesn't exist.

However, the error generated when Group is not found usually comes
from Memcache.

Can you send more information on your environment? Version of Rails,
version of Ruby, version of memcache-client, revision of
acts_as_cached (if you know), and, finally, perhaps the
GeneralController#index code?

Thanks.


--
Chris Wanstrath
http://errtheblog.com

fightinjoe

unread,
Feb 6, 2007, 3:57:31 AM2/6/07
to acts_as_cached
Chris, I switched to cache_fu and still had the same problem.

I'm using Rails 1.2.1, Ruby 1.8.4, memcache-client 1.2.1, and I
checked out cache_fu today.

The general controller isn't doing anything funky:

++++++++++++++++++++++++++++++

class GeneralController < ApplicationController
before_filter :login_required

def index
@user = current_user
end
end

++++++++++++++++++++++++++++++

current_user comes from authentication_system. The following is the
main line where current_user is being found:

++++++++++++++++++++++++++++++

def logged_in?
(@current_user ||= session[:user] ?
User.get_cache(session[:user]) : :false).is_a?(User)
end

++++++++++++++++++++++++++++++

I was able to get the autoloading working when I did the following to
line 56 of /lib/acts_as_cached/cache_methods.rb:

++++++++++++++++++++++++++++++

rescue ArgumentError, MemCache::MemCacheError => error

++++++++++++++++++++++++++++++

If you're curious, here is the backtrace where things start to go
wrong:

++++++++++++++++++++++++++++++

lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:904:in
`load'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:904:in
`restore'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:805:in
`fetch'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:1035:in
`send'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:1017:in
`send'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:795:in
`fetch'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:926:in
`add_stat'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:783:in
`fetch'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:345:in
`get'
lib/ruby/1.8/sync.rb:229:in `synchronize'
lib/ruby/gems/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb:344:in
`get'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
cache_methods.rb:50:in `fetch_cache_without_benchmarking'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
cache_methods.rb:55:in `autoload_missing_constants'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
cache_methods.rb:49:in `fetch_cache_without_benchmarking'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
benchmarking.rb:30:in `fetch_cache'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
benchmarking.rb:18:in `cache_benchmark'
#{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/
benchmarking.rb:37:in `silence'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
benchmarking.rb:18:in `cache_benchmark'
/lib/ruby/1.8/benchmark.rb:293:in `measure'
/lib/ruby/1.8/benchmark.rb:307:in `realtime'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
benchmarking.rb:17:in `cache_benchmark'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
benchmarking.rb:29:in `fetch_cache'
#{RAILS_ROOT}/vendor/plugins/cache_fu/lib/acts_as_cached/
cache_methods.rb:18:in `get_cache'
#{RAILS_ROOT}/lib/authenticated_system.rb:6:in `logged_in?'
#{RAILS_ROOT}/lib/authenticated_system.rb:83:in `login_required'

On Feb 6, 4:54 pm, "Chris Wanstrath" <c...@ozmm.org> wrote:


> On 2/5/07, fightinjoe <fightin...@gmail.com> wrote:
>
> > When I do this, I get the following error:
>
> > ArgumentError in GeneralController#index: undefined class/module Group
>
> > Before I start trying to figure out Rails's auto-include hooks, does
> > anyone have any insight as to a better way to deal with this issue?
>
> This is interesting because acts_as_cached has code to trigger Rails'
> autoloading in the situation you describe. Specifically:
>

> http://require.errtheblog.com/plugins/browser/cache_fu/lib/acts_as_ca...

Chris Wanstrath

unread,
Feb 6, 2007, 7:04:53 PM2/6/07
to acts_as...@googlegroups.com
On 2/6/07, fightinjoe <fight...@gmail.com> wrote:

> I'm using Rails 1.2.1, Ruby 1.8.4, memcache-client 1.2.1, and I
> checked out cache_fu today.

Looks like you're actually using Ruby-MemCache-0.0.1, not
memcache-client (according to the backtrace).

You might have both installed? I should probably make init.rb require
memcache-client or something. Here we include it in our
config/environment.rb.

> I was able to get the autoloading working when I did the following to
> line 56 of /lib/acts_as_cached/cache_methods.rb:
>
> ++++++++++++++++++++++++++++++
>
> rescue ArgumentError, MemCache::MemCacheError => error

Either way, I'm going to go ahead and add this in. I expect there
will probably be some other bugs you encounter due to discrepencies
between memcache-client and Ruby-Memcache. Let me know which ones you
run across.

http://require.errtheblog.com/plugins/changeset/101

Reply all
Reply to author
Forward
0 new messages