-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Caching a one-to-many collection like a "friends list"
PostPosted: Mon Jan 25, 2010 5:41 pm 
Newbie

Joined: Mon Jan 25, 2010 5:22 pm
Posts: 3
I have an application very much like a social networking "friends list". We have a "User" hibernate entity, and each User can have a bunch of other Users as their friend. This list is represented by a table in the database, whose columns are an entryid, fk_source_user, fk_target_user, where the foreign keys link to a user table. Each source user could have 0..N target Users as their friends. My application often wants to know the list of all friends for a given User.

This is something that smells like it could be cached very effectively by a second-level cache. But I'm trying to understand how to best achieve that with hibernate's caching mechanism. My first thought was to use the query cache, eg, cache queries like this:

Code:
SELECT targetid FROM friendlist WHERE sourceid = 'x'


Which returns all the friend ids for a given User. However my understanding of the query cache is that the cache would be cleared any time the friendlist table is updated, which could be quite often if we have a lot of users. So using a query cache would result in a lot of cache misses.

Since the list of friends for a given User should be fairly static, I think there must be some way to cache this without having to discard it each time the friendlist table is updated, but without using the query cache, I'm not sure how I would do this.

Does anyone have a suggestion for how I can effectively cache this information?


Top
 Profile  
 
 Post subject: Re: Caching a one-to-many collection like a "friends list"
PostPosted: Tue Jan 26, 2010 5:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You can declare in the mapping document that a collection should be cached. See http://docs.jboss.org/hibernate/stable/ ... he-mapping for some examples
or http://docs.jboss.org/hibernate/stable/ ... ml#d0e2911 if you are using annotations.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.