Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement redis cache action example #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Voycawojka
Copy link
Contributor

Description

Implement redis cache action example

Motivation and Context

This example was missing

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

I hereby agree to the terms of the Knot.x Contributor License Agreement.

@Test
@DisplayName("Expect 200 status code when endpoint registered.")
void callUndefinedRoute() {
given()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is obsolete, the same action is invoked by the Docker

this.payloadKey = getPayloadKey();

JsonObject cacheConfig = config.getJsonObject("cache");
ttl = cacheConfig != null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be Optional ;)

}

public static String serializeObject(Object object) {
if (object instanceof JsonObject) {
Copy link
Contributor

@Mateusz512 Mateusz512 Nov 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use .toString 💡
since toString implementations of JsonArray and JsonObject both invoke their own .encode methods

@@ -0,0 +1,55 @@
[![][license img]][license]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove those two lines.

New value cached under key: the-book for 20 seconds
```

[license]:https://github.com/Cognifide/knotx/blob/master/LICENSE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove lines below.


@Test
@DisplayName("Expect 200 status code when endpoint registered.")
void callUndefinedRoute() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the method name if this test is still valid.


<logger name="io.knotx" level="DEBUG"/>
<logger name="io.vertx" level="DEBUG"/>
<logger name="com.codahale.metrics" level="TRACE"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we copy those logger. Please remove it.

config {
redis.host = redis #it's available under this host in docker compose/swarm
cache.ttl = 20
cacheKey = "the-book"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cacheKey = "the-book"
cacheKey = the-book

}

private RedisOptions getRedisOptions(JsonObject config) {
return Optional.ofNullable(config.getJsonObject("redis"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we load Redis options directly from JSON? Currently we are not able to set many options such as encoding, authentication etc.

I would see in configuration

redisOptions {
  host = "some value"
  ...
}

And then we would use the RedisOptions(JsonObject json) constructor to fill all fields. It comes with one additional benefit. The new versions of Vert.x Redis library can introduce new options. We would not have to modify this code when we would use JSON.

private final Action doAction;

RedisCacheAction(Vertx vertx, JsonObject config, Action doAction) {
this.config = config;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we introduce RedisCacheActionOptions ? Then all defaults can be initialised in some init() method.

/**
* Action factory for caching fragment payload values on Redis server. Can be initialized with a configuration:
* <pre>
* productDetails {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

productDetails ? Do you plan to migrate to api-gateway/caching.

* cache.ttl - in seconds, default value: 60
* </pre>
*/
public class RedisCacheActionFactory implements ActionFactory {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... what about @Cacheable. Please check when action's instance is created: io.knotx.fragments.handler.action.ActionProvider#isCacheable. In my opinion the current solution does not work.

public void apply(FragmentContext fragmentContext, Handler<AsyncResult<FragmentResult>> resultHandler) {
String cacheKey = getCacheKey(fragmentContext.getClientRequest());

redisClient.connect(onConnect -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We create the new connection each time the action is invoked. Can we reuse the connection? What about reconnecting on error?

See:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants