1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# p80 of cache_fu RailsConf07 slides
user_ids = @topic.posts.map(&:user_id).uniq
@users = User.get_caches(user_ids) # I prefer this as it guarantees an Array
# Trivial question:
# With normal AR, @topic.posts will load up all the Post records, without any caching
# So perhaps instead:
user_ids = @topic.posts.find(:all, :select => 'user_id').map_by_user_id.uniq
# Less trivial:
# Are we getting to the point where we're no longer using AR api?
# We don't use find:
User.find(user_ids) => User.get_caches(user_ids)
# We don't use nice association syntax:
@users = @topic.posts.map_by_user.uniq
# As pretty as cache_fu is, I've started writing explicit wrapper methods to build
# associations via caching.
# It disturbs me.
# Could AR or Ambition be used to map all these look ups to cache requests?
# If we did this, the syntax could stay the same, you just turn on caching,
# and you get the automatic expiration via SomeClass.after_save :reset_cache
# The only thing not cached is associations.
# But, could the AR syntax be automatically mapped to mapping of ids, and then
# cache lookups as above?
# Messing with AR scares me. I've done it enough for one lifetime... especially anything
# to do with associations.
# Would it be easier to do transparently within Ambition?
|
Pastie
