Reputation: 571
Sorry for the poor title but I didn't know how else to phrase my use case.
I'm trying to use a Guava cache to load user profile objects keyed by their IDs. The catch is that the profiles may change over time, so I need to key the request by the date as well. Further, I'd only like to cache a single profile for a single user (instead of 7 different profiles for every day of the week for a single user).
Is there any way to replace existing cache entries with newly loaded ones only if the date changes, instead of adding a new cache entry for the new unique key?
For clarity:
A sample key would look like <user id, date>
If I have a cached entry that is keyed by <123, "2013-02-13">
, and a request comes in for <123, "2013-02-14">
, there should only be one entry in the cache for user 123 after loading the new profile.
Thanks!
Upvotes: 1
Views: 458
Reputation: 198014
It sounds like what you should be doing is to have a Cache<UserId, DateAndProfile>
, and then to check yourself if the DateAndProfile
needs to be overwritten. The Guava caching API isn't going to let you treat different keys as "sort of the same" in any fancy way.
Upvotes: 2