To implement a caching mechanism in a SpringBoot application, I need to generate a collision free kind of identity fingerprint of objects of certain classes. This fingerprint shall not include all class attributes, because some of them need to be intentionally kept out from calculation because they do not describe the identity of the instance in point of view of the caching. As hashing algorithm, I selected sha-256.
The question, which concerns me, is not about the hashing algorithm itself but rather how to make sure that attributes, which are added later to the class (even in nested referenced classes) are not forgot to either intentionally include or exclude in the hash calculation. Currently, in the first MVP approach, I added methods (String identityHash() {...}
) to the related classes, which contains hardcoded attribute value concatenations, which are hashed then.
However, I would rather like an Annotation based approach: Each attribute of the related classes shall have Annotations, which express, whether to include the attribute value in the hash calculation or not.
public class MyEntity {
@IdentityHash(include=false)
String id
@IdentityHash(include=true)
String attribute1
@IdentityHash(include=true)
MySubEntity subEntity
}
public class MySubEntity {
@IdentityHash(include=false)
String id
@IdentityHash(include=true)
Set<String> setOfValues
}
Rather than having identityHash
methods on the classes, I could implement a Service, which calculates the corresponding hashes. But here I'am not sure about which is the correct way to go. One option would be: use reflection (e.g. BeanUtils, PropertyUtils) to enumerate the attributes (recursively!), build the hash input by concatenating their value representatives (e.g. Sets need to be sorted first...) Would reflection be OK in such an use case? Are there other possibilities? I imagine something like ObjectMapper, which either builds a String (or byte[]) representation to calculate the hash then...
Aucun commentaire:
Enregistrer un commentaire