AgRest + Cayenne + JCache + Hazelcats or EhCache example

24 views
Skip to first unread message

Сергей Нелюбин

unread,
Jul 26, 2019, 1:52:56 PM7/26/19
to Agrest Framework User Group
Hello, Andrus!

Where i can find example with AgRest + Cayenne + JCache + Hazelcats or EhCache?

1) I add top pom.xml
<dependency>
<groupId>org.apache.cayenne</groupId>
<artifactId>cayenne-jcache</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cayenne</groupId>
<artifactId>cayenne-cache-invalidation</artifactId>
<version>4.0.1</version>
</dependency>

and 
<property name="cayenne.DataDomain.sharedCache" value="true"/>

but the log file does not contain records about the fact of adding objects to the hazelcast cache

2) if i add
.stage( SelectStage.ASSEMBLE_QUERY,  selectContext -> {
selectContext.getEntity().getSelect().useSharedCache("QwertyCache");
} )
to @GET method, then cayenne try load objects from cache, but result is empty

In @POST method, when Agrest methos syncAndSelect invoked - cayenne does not try add objects to hazelcast cache.

What am I mistaken?

Andrus Adamchik

unread,
Jul 29, 2019, 11:31:37 AM7/29/19
to Agrest Framework User Group
Hi Sergey,

EhCache works well for me. Hazelcast should too, though I don't have much firsthand experience with it. I created a "caching" branch in one of the examples that shows what needs to be done assuming you are on EhCache [1].

What you did is actually correct. You just need a few tweaks:

1. Add a JCache provider dependency to your POM. E.g.:

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.0</version>
</dependency>

2. For cache invalidation to happen on entity changes, annotate your entity with @CacheGroups, using the same cache group you have in your AgRest endpoint ("QwertyCache" in your example, "categories" in mine) :

@CacheGroups("categories")
public class Category extends _Category {
}

Once you do 1 & 2, your caching will work, and the results will stay in the cache until invalidated.

3. To additionally configure cache sizes and expiration policies, add "ehcache.xml" file [2], and let Cayenne know on startup [3]:

URL config = getClass().getClassLoader().getResource("ehcache.xml");
ServerRuntime cayenneRuntime = ServerRuntime.builder()
.addConfig("cayenne-project.xml")
.addModule(b -> JCacheModule.contributeJCacheProviderConfig(b, config.toString()))
.build();

4. While "useSharedCache" should work, I recommend using "useLocalCache". Since all GET operations in AgRest are using the same context, this will be much faster.

HTH
Andrus

[1] https://github.com/agrestio/agrest-bookstore-example/tree/caching
[2] https://github.com/agrestio/agrest-bookstore-example/blob/caching/src/main/resources/ehcache.xml
[3] https://github.com/agrestio/agrest-bookstore-example/blob/caching/src/main/java/org/example/agrest/BookstoreApplication.java
> --
> You received this message because you are subscribed to the Google Groups "Agrest Framework User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to agrest-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/agrest-user/2a955282-ca6f-4d69-8a3e-af94e62bec21%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages