Cached Associations

11 views
Skip to first unread message

Eli Miller

unread,
Aug 8, 2007, 9:03:06 PM8/8/07
to acts_as...@googlegroups.com
Here's a patch I've been playing with for a few days. I'm using it one
of my apps and it seems to be working well although it's not very well
tested yet.

I'd like to get this included in cache_fu eventually but I'm looking
for early thoughts on implementation problems or concerns.

Main features:

* Allows cache_fu to use something other than .id for the "id" of a
cached model. This is accessible through your own cache_id method or
doing "acts_as_cached :id => :user_id". Make sure that the id you're
about to use is unique, and you'll probably want to set the finder to
something like :find_by_user_id.

* Provides "acts as cached :find_by => :user_id" that automatically
sets the finder and overrides the cache_id method for you.

* Two simple methods to add associations with cache_fu finders to your
models. A user might has_one a settings model, which belongs_to the
user. By using belongs_to_cached and has_one_cached we can
automatically use the get_cache method to find those objects. Be
careful because has_one_cached assumes that the model referenced is
cached on the this models id. See the example below.

Here's a better example (untested hand written code):

class User < ActiveRecord::Base
acts_as_cached

has_one_cached :settings
end

class Settings < ActiveRecord::Base
acts_as_cached :find_by => :user_id

belongs_to_cached :user
end

>> User.get_cache(1).settings
==> Got User:1 from cache. (0.00047)
==> Got Settings:1 from cache. (0.00056)

>> Settings.get_cache(1).id
=> 5000
>> Settings.get_cache(1).cache_id
=> 1
>> Settings.get_cache(1).cache_key
=> "Settings:1"
>> Settings.get_cache(1).expire_cache
==> Deleted Settings:1 from cache. (0.00040)

cache_fu_improvements

Chris Wanstrath

unread,
Aug 16, 2007, 4:38:08 PM8/16/07
to acts_as...@googlegroups.com
I like these features and would really love some specs so we could
roll these into the plugin.

- chris

Eli Miller

unread,
Aug 18, 2007, 2:29:33 AM8/18/07
to acts_as...@googlegroups.com
On 8/16/07, Chris Wanstrath <ch...@ozmm.org> wrote:
> I like these features and would really love some specs so we could
> roll these into the plugin.

I'm trying to write some test cases for this code, but it seems to be
pretty tricky to use ActiveRecord without a database. Do you have any
recommendations on how I might go about this, or maybe I should just
run the tests that require a database if a database is configured?

Chris Wanstrath

unread,
Aug 18, 2007, 5:03:16 AM8/18/07
to acts_as...@googlegroups.com
On 8/17/07, Eli Miller <elijah...@gmail.com> wrote:

> I'm trying to write some test cases for this code, but it seems to be
> pretty tricky to use ActiveRecord without a database. Do you have any
> recommendations on how I might go about this, or maybe I should just
> run the tests that require a database if a database is configured?

Most of the cache_fu tests make pretty heavy use of Mocha for avoiding
databases and, indeed, ActiveRecord itself. If they aren't helping you
with what you need to do, feel free to contact me off list with your
database-enabled tests and I can help Mochanize them.

- chris

Reply all
Reply to author
Forward
0 new messages