Search API and various types in one cache

81 views
Skip to first unread message

Mars Man

unread,
Apr 12, 2016, 6:54:17 AM4/12/16
to ehcache-users
I would like to use the Search API of EhCache 2.8 to invalidate (remove) a group of cache entries from cache. In my cache different types of elements and therefor keys are stored. Products, Items, Badges and more. How do I have to define the search attributes in ehcache.xml? I tried one attribute with expression like "key.getProductId()". But putting the first non product element on the cache results into:

net.sf.ehcache.search.attribute.AttributeExtractorException: No such method named "getProductId" present on instance of class ...

How can I solve this problem? Do I have to define a separate cache for each type of element??

Here is a part of my configuration and code:

<searchable keys="true" values="false">
 
<searchAttribute name="productId" expression="key.getProductId()" type="Long"/>
</searchable>

Query query = getCache().createQuery().addCriteria(getCache().getSearchAttribute("productId").eq(id)).includeKeys().end();


Fabien Sanglier

unread,
Apr 12, 2016, 3:05:09 PM4/12/16
to ehcach...@googlegroups.com
Of course one solution is to use different caches for different types of objects. If you do that, then it should all work fine using out of box configuration notation.

But if that's not possible or practical, you can go the custom search extractor route where you could register it in the cache, and pick and chose (in custom extractor code) the data to be indexed (and using things like typeof to do A if objects is of type A, and do B if object is of type B etc...

Fabien
--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/07949060-e156-4cd3-9058-58bfbf3e7215%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Fabien Sanglier
fabiens...@gmail.com
Message has been deleted

Mars Man

unread,
Apr 13, 2016, 3:12:41 AM4/13/16
to ehcache-users
Thank you for your reply. I've tried the approach with custom extractor in the meanwhile and it solves the problem. But do I have to understand your 2. solution as a recommendation to use one cache for each type of object? Is this a typical setup for EhCache?

Titus


Am Dienstag, 12. April 2016 21:05:09 UTC+2 schrieb Fabien Sanglier:Of course one solution is to use different caches for different types of objects. If you do that, then it should all work fine using out of box configuration notation.

James House

unread,
Apr 13, 2016, 2:54:09 PM4/13/16
to ehcache-users
Yes, a cache per data type (object class) is typical.

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.

Louis Jacomet

unread,
Apr 13, 2016, 2:58:06 PM4/13/16
to ehcache-users
To add to that, both JSR-107, the standard for caching in Java, and the upcoming Ehcache 3 do offer an API with generic types on caches.

Regards,
Louis

Reply all
Reply to author
Forward
0 new messages