I want to ask if there is a better way to search in an ehcache. I've created a cache with key and list of values, the list is a arraylist of strings. Now I want to search wihtin the list of strings e.g. email adresses and return the whole java object.
key: ID12345678 value: List of emails [test@test.de, test1@test.de, ...]
Currently I do as following:
public static class EMailAttributeExtractor implements AttributeExtractor {
public Object attributeFor(Element element) {
VzdData vzdData = ((VzdData) element.getObjectValue());
return String.join(";", vzdData.getServiceData().getMail());
}
@Override
public Object attributeFor(Element element, String attributeName) throws AttributeExtractorException {
return this.attributeFor(element);
}
}
Searchable searchable = new Searchable();
searchable.addSearchAttribute(new SearchAttribute().name("email").className(EMailAttributeExtractor.class.getName()));
searchable.setAllowDynamicIndexing(true);
public VzdData getVzdDatabyEMail(String email) throws VZDException {
log.entry(email);
VzdData vzdData = null;
Attribute<String> emailSearchAttribute = this.cache.getSearchAttribute("email");
Query query = this.cache.createQuery().addCriteria(emailSearchAttribute.ilike("*" + email + "*"));
query.includeValues();
Results results = query.execute();
List<Result> resultList = results.all();
return log.exit(vzdData);
}
I wrote a test case, was looking into the source code, but don't find a better solution as to flatten the list to a comma separated string a do a like search.