-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: master
Are you sure you want to change the base?
Implement redis cache action example #64
Conversation
@Test | ||
@DisplayName("Expect 200 status code when endpoint registered.") | ||
void callUndefinedRoute() { | ||
given() |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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"/> |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cacheKey = "the-book" | |
cacheKey = the-book |
} | ||
|
||
private RedisOptions getRedisOptions(JsonObject config) { | ||
return Optional.ofNullable(config.getJsonObject("redis")) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 -> { |
There was a problem hiding this comment.
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:
Description
Implement redis cache action example
Motivation and Context
This example was missing
Types of changes
Checklist:
I hereby agree to the terms of the Knot.x Contributor License Agreement.