Caching a model that belongs to another model


#1

Hi everyone…
I have a user model that stores the currently logged in user in a
session variable so a db query won’t be created for every access to it.
However, I have another model (user_properties) that belongs_to the user
model. Question is, how can I achieve the same caching effect when I
access
current_user.user_properties (which is a dynamic finder I think)?

It currently creates a query for the user_properties based on the cached
user - but I want that data to be cached as well.

Any thoughts?
Thanks!


#2

If you cache much user data, you run the security risk of a session
hijack exploit (to name one). Additionally, these queries tend to be
lightweight and databases optimize for recent queries … so if the
cost of a query is 1, the cost of 100 queries all exactly alike is
significantly less than 100.

If you really, really want to cache this stuff then do something like
this to your user model:

class User

This solution has some naivety about your problem. If your user

has_many properties, then you’ll have to

create an array to hold them instead of a nil scalar.

@@_user_properties = nil

def user_properties
@@_user_properties = self.send(user_properties) if
@@_user_properties.nil?
@@_user_properties
end
end

My belief is that doing this will not reduce your server load
meaningfully, and it opens a pretty big security hole.

HTH


#3

Thanks!
That’s what I was looking for…


#4

On 4/22/07, Ehud R. removed_email_address@domain.invalid wrote:

user - but I want that data to be cached as well.

Any thoughts?
Thanks!

It already is cached. tail -f log/development.log and then open up
the console. Watch the log file as you run these queries
u = User.find 1 # SELECT …
u.user_properties # SELECT …
u.user_properties # no query

Rails caches the association, so you don’t have to worry about it.

You can reduce the number of queries down to one by using the :include
option to find.

u = User.find 1, :include => :user_properties

Rails will do a join and pull all the info in one query.

Pat