Hi,
I'm using ehcache (via the Grails plugin). The method that adds objects to the cache requires the keys to be serializable, so a typical usage would be:
def key = 22
def someObject = new Object();
cacheService.cache(key, true, someObject)
(The boolean param indicates whether the object should be added to a distributed or local cache)
My question is how I should go about generating keys from value objects such as:
class Person implements Serializable {
String firstName
String lastName
Integer age
}
One approach would be to provide a hashCode() and equals() methods and use the hashCode as the key. In this case, I wouldn't need to make the Person class implement Serializable.
Alternatively, I could simply use the Person object itself as the key. It seems like I would still need to provide the equals and hashCode methods but would also need to implement Serializable. However, there would seem to be a smaller chance of collisions using this approach because a Person can only be equal to another instance of Person.
I'm assuming that ehcache uses the equals() method of a key to determine whether that key already exists in the cache, is this assumption correct?
Is either of the approaches outlined above intrinsically better than the other, or is there another approach that I haven't considered?
Thanks, Don