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?