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)
- chris
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?
> 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