title: cache key generation
body: <p>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:</p>
  
  <pre><code>def key = 22
  def someObject = new Object();
  cacheService.cache(key, true, someObject)
  </code></pre>
  
  <p>(The boolean param indicates whether the object should be added to a distributed or local cache)</p>
  
  <p>My question is how I should go about generating keys from value objects such as:</p>
  
  <pre><code>class Person implements Serializable {
    String firstName
    String lastName
    Integer age
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Is either of the approaches outlined above intrinsically better than the other, or is there another approach that I haven't considered?</p>
  
  <p>Thanks,
  Don</p>