Red Hat 㧠Solution Architect ã¨ã㦠Quarkus ãæ å½ãã¦ããä¼è¤ã¡ã²ãï¼@chiroitoï¼ã§ãã
ãã®è¨äºã¯ãQuarkus.io ã®ããã°è¨äºãUsing Redis with Mutiny - Composing asynchronous actions ã®ç¿»è¨³è¨äºã§ãã
ã¦ã¼ã¶ã¼ãã Redis 㨠Mutiny ã«ã¤ãã¦èå³æ·±ã質åãåãã¾ããããã®åé¡ã¯ç¹ã« Redis ã«é¢ãããã®ã§ã¯ãªããä»ã®å¤ãã® API ã«ãé©ç¨ã§ãããã®ã§ããããç§ã¯ãã®æèãé¢ç½ãã¨æãã¾ããã
ã¦ã¼ã¶ã¼ã® Enrico ã¯ããããªãã¨ããããã¨æã£ã¦ãã¾ããï¼
1. Redis ãããã¹ã¦ã®ãã¼ãåå¾ 2. åãã¼ã«å¯¾ã㦠-> é¢é£ãããªãã¸ã§ã¯ããåå¾ 3. ãã®ãªãã¸ã§ã¯ãã JsonArray ã«è¿½å 4. ãã¹ã¦ã®ãªãã¸ã§ã¯ããå«ã JsonArray ãçæ
Enrico 㯠Vert.x Redis ã¯ã©ã¤ã¢ã³ãã® Mutiny åã使ç¨ãã¦ãã¾ãã
ãã®ã¯ã©ã¤ã¢ã³ãã¯ãç§ãã¡ã®åé¡ã解決ããããã«ãããã¤ãã®ã¡ã½ãããæä¾ãã¦ãã¾ãã
RedisClient.keys(pattern)
ã¡ã½ããã¯Uni<JsonArray>
ãè¿ãã¾ãããã®é åã«ã¯ãkeys
ã¡ã½ããã«æ¸¡ããããã¿ã¼ã³ã«ä¸è´ãããã¼ã®ãªã¹ããå«ã¾ãã¾ãããã®è¨äºãç°¡åã«ããããã«ããã¹ã¦ã®ãã¼ãè¿ãï¼keys("*")
ã使ç¨ãã¦ã¿ã¾ããããRedisClient.hgetall(key)
ã¡ã½ããã¯Uni<JsonObject>
ãè¿ãã¾ãããã®ã¡ã½ããã¯ã渡ããããã¼ã«é¢é£ä»ãããããªãã¸ã§ã¯ããåå¾ãã¾ãã
ã©ã¡ãã®ã¡ã½ãããéåæï¼Uniãè¿ãï¼ã§ãããªã®ã§ãåå¾ãããã¼ãã¨ã« 2 çªç®ã®ã¡ã½ãããå¼ã³åºãå¿
è¦ãããã¾ããè¨ãæããã°ããã¼ã®ã»ãããå復å¦çããåãã¼ã«å¯¾ãã¦éåæã¢ã¯ã·ã§ã³ãå¼ã³åºãå¿
è¦ãããã¾ããæå¾ã«ããããã®éåæã¢ã¯ã·ã§ã³ã®çµæã JsonArray
ã«åéãããã¨æãã¾ãã
ã¾ãã¯æåãããã£ã¦ã¿ã¾ãããï¼Redis ã¯ã©ã¤ã¢ã³ãã®ã¤ã³ã¹ã¿ã³ã¹ãå¿ è¦ã§ãã
RedisClient redis = RedisClient.create(vertx, new JsonObject() .put("port", 6379) .put("host", "localhost"));
Quarkus ã§ã¯ãRedis æ¡å¼µã¢ã¸ã¥ã¼ã«ãç´æ¥ä½¿ç¨ããå¿ è¦ãããã¾ããããã¯åæ§ã® API ãå ¬éãã¦ãã¾ããEnrico 㯠Vert.x Redis ã¯ã©ã¤ã¢ã³ããç´æ¥ä½¿ãããã¨æã£ã¦ãã¾ããã
ã¯ã©ã¤ã¢ã³ããæã«å ¥ããã®ã§ããã¼ã®ãªã¹ããåå¾ãã¦ã¿ã¾ãããã
Uni<JsonArray> keys = redis.keys("*")
ãã㯠JsonArray
ãçæãã¾ããã§ããããã¼ã®ã¹ããªã¼ã ãå¿
è¦ã§ããç¹°ãè¿ãã«ãªãã¾ãããããã¯éåæã¡ã½ããã§ããè¿ããã Uni
ã¯å©ç¨å¯è½ãªã¨ãã«é
åãåãåãã¾ãã(onItem
) ãä¸åº¦åãåãã¨ããã®é
åããã¹ããªã¼ã ãä½æã§ãã¾ãã
Multi<String> keys = redis.keys("*") .onItem().transformToMulti(array -> Multi.createFrom().iterable(array)) .onItem().castTo(String.class);
ãã®ã¹ããããï¼
- ãã¼ãå«ã
JsonArray
ãåå¾ãã¾ãã - ãããã®ãã¼ãã¹ããªã¼ãã³ã°ãã
Multi
ãä½æãã¾ããããã¯JsonArray
ãIterable<Object>
ãæ¡å¼µãã¦ããã®ã§Multi<Object>
ã§ãã - ãã®
Multi
ã¢ã¤ãã ãString
ã«å¯¾å¿ä»ããã¾ãã
ãã®æç¹ã§ã¯ã(String)ãã¼ã®ã¹ããªã¼ã ãããã¾ããã¨ãããã¨ã§ãã¹ããã1ã¯ããã§çµäºã§ãã
ãã¦ãã¹ããã2ï¼åãã¼ã«ã¤ãã¦ãé¢é£ãããªãã¸ã§ã¯ããåå¾ãããã¨æãã¾ãã
ããã§ãhgetall
ã¡ã½ããã使ã£ã¦ã¿ã¾ãããï¼
Multi<JsonObject> objects = keys .onItem().transformToUniAndMerge(key -> redis.hgetall(key));
ãã®ã¹ããããã¯å°ã説æãå¿ è¦ã§ãã
ãã¼
ã®ã¹ããªã¼ã ã®åã¢ã¤ãã ã«ã¤ãã¦ãhgetall
ãå¼ã³åºã㦠Uni<JsonObject>
ãçæãã¾ãã
ããã§ããã¼ã Uni ã«å¤æãããã¨æãã¾ã(transformToUni)ã
ã¢ã¤ãã ã®ã¹ããªã¼ã ããããåã¢ã¤ãã ã«å¯¾ãã¦éåæã¢ã¯ã·ã§ã³ãå¼ã³åºãå¿ è¦ãããå ´åãçµæãã©ã®ããã«ãã¼ã¸ããããé¸æããªããã°ãªãã¾ãããMutiny ã¯2ã¤ã®æ¦ç¥ãæä¾ãã¾ãã
- ãã¼ã¸ -
Uni
ã§çç£ãããã¢ã¤ãã ãåä¿¡ãããããã«ãããä¸æµã«éãã¾ãã - é£çµ - å ¥åã¹ããªã¼ã ã®é åºãä¿æãã¦ãä¸æµã«éãããã¢ã¤ãã ãåãé åºã§éãããããã«ãã¾ãã
ãããå³è§£ãã¦ã¿ã¾ããããæã
ã¯ãã¼1
ã2
ã3
ãæã¡ããããã¹ããªã¼ã {1ã2ã3}
ã«æã£ã¦ããã¨æ³åãã¦ã¿ã¦ãã ãããã¾ããç§ãã¡ã® Redis ãã¼ã¿ãã¼ã¹ã§ã¯ããã¼1
ã A
ã2
ã B
ã3
ã C
ã«é¢é£ä»ãããã¦ããã¨èãã¦ã¿ã¾ãããã
ãã¼ã¸æ¦ç¥ã使ç¨ããå ´åãé¢é£ãããªãã¸ã§ã¯ããä¸ç¢ºå®ãªé åºã§åå¾ãã¦ãã¾ããæçµçã«ã¯{A, C, B}
ã¾ãã¯{B, A, C}
ã§çµããã¾ããããã¯ãã¬ã¤ãã³ã·ãã¹ã±ã¸ã¥ã¼ãªã³ã°ãè² è·ãªã©å¤ãã®è¦å ã«ä¾åãã¾ããããããããã¯ãé¢é£ãããã¹ã¦ã®ãªãã¸ã§ã¯ãã並åã«åå¾ããé åºãæ°ã«ããã«çµæã®ã¹ããªã¼ã ãçæã§ãããã¨ãæå³ãã¾ãã
é£çµæ¦ç¥ã使ç¨ããã¨ãããã¯å
¥åã¹ããªã¼ã ããã®é åºãä¿æãã¾ãããªã®ã§ãå¿
ã{A,B,C}
ãåºã¦ãã¾ãããããæã¾ããããããã¾ããããããã¯åæã«ãªãã¸ã§ã¯ããåå¾ããè½åãä½ä¸ãããå¯è½æ§ãããã¾ããããã¯ãMutinyãåã®ãªãã¸ã§ã¯ãã®ãã¹ã¦ã®æ¤ç´¢ãå¾
ããªããã°ãªããªããããªãã®ã§ããä¾ãã°ãMutiny ãå
ã« C ãåä¿¡ããå ´åãC ãä¸æµã«éãåã« A 㨠B ãå¾
ã¤å¿
è¦ãããã¾ãã
ãã®æèã§ã¯ãé åºãä¿æããã«ãã¼ã¸æ¦ç¥ã使ç¨ãã¾ããããããã§ãtransformToUniAndMerge
ã使ç¨ãã¾ãã
ã³ã¼ããä½åº¦ãå®è¡ããã¨ãçµæã®é åã®é åºãå¤ããããããã¾ããã
ã¹ããã2ã¯å®äºãã¾ãããæå¾ã®ã¹ãããã«ç¦ç¹ãå½ã¦ã¦ã¿ã¾ãããï¼ãªãã¸ã§ã¯ãã JsonArray
ã«èç©ãããã¹ã¦ã®ãªãã¸ã§ã¯ããå«ã Uni<JsonArray>
ãçæãã¾ããMutiny ã¯ã¹ããªã¼ã ããã¢ã¤ãã ããªã¹ããããããã»ããã«åéããã¡ã½ãããæä¾ãã¦ãã¾ããã§ãããçµã¿è¾¼ã¿ã® JsonArray
ã®ãµãã¼ãã¯ããã¾ããã幸ããªãã¨ã«ãMutiny ã¯ããªããã©ã®ãããªæ§é ã®ã¢ã¤ãã ãåéããããã«ã使ç¨ã§ããã¡ã½ãããæä¾ãã¦ãã¾ãã
Uni<JsonArray> result = objects
.collectItems().in(() -> new JsonArray(), (arr, obj) -> arr.add(obj));
collectItems().in
ã¯ãç¬èªã®æ§é ã«ã¢ã¤ãã ãèç©ã§ãã¾ããããã¯2ã¤ã®ãã©ã¡ã¼ã¿ãåãã¾ããä¸åº¦ã ãå¼ã³åºãããæ§é ã® supplier ã¨ãæ§é ã¨è¿½å ããã¢ã¤ãã ãåãåã bi-consumer ã§ãåã¢ã¤ãã ãã¨ã«å¼ã³åºããã¾ãã
ãããè¡ãã¾ãããããã㧠Enrico ã®çåã解決ã§ãã¾ãã
ãªã¼ã«ã¤ã³ã¯ã³ã®ã³ã¼ãã¯ä»¥ä¸ã®éãã§ãï¼
Uni<JsonArray> result = // ã¹ããã1 - ãã¼ãåå¾ redis.keys("*") .onItem().transformToMulti(keys -> Multi.createFrom().iterable(keys)) .onItem().castTo(String.class) // ã¹ããã2 - åãã¼ã«é¢é£ä»ãããããªãã¸ã§ã¯ããåå¾ .onItem().transformToUniAndMerge(key -> redis.hgetall(key)) // ã¹ããã3ã¨4 - åå¾ãããªãã¸ã§ã¯ããJsonArrayã«èç© .collectItems().in(() -> new JsonArray(), (arr, obj) -> arr.add(obj));
ãã®ã¹ããããã§ã¯ãããã¤ãã®é¢ç½ããã¿ã¼ã³ãããã¾ãï¼
- ã³ã¬ã¯ã·ã§ã³ãæã£ã¦ãã¦ãããã Mutiny ã§å復ãããå ´åã¯ãããã
Multi
ã«å¤æãã¾ãã - ã¹ããªã¼ã ã®åã¢ã¤ãã ã«å¯¾ãã¦éåæã¢ã¯ã·ã§ã³ãå®è¡ããå ´åããã¼ã¸ã¨é£çµã«ã¤ãã¦èãã¦ã¿ã¦ãã ãããèªåã«ã¨ã£ã¦æå³ã®ãããã®ã使ãã¾ãããã
- ã¢ã¤ãã ãæ§é ã«èç©ããã«ã¯ã
collectItems
ã使ç¨ãã¾ããããã¯ããªãã®é¸æã®æ§é ãçæããããã®å¤ãã®ã¡ã½ãããæä¾ãã¦ãã¾ãã
ãã®ã³ã¼ããå®éã«åä½ãã¦ããã®ãè¦ããå ´åã¯ããã® gist ã確èªãã¦ãã ãããJBang ã§ç´æ¥å®è¡ãããã¨ãã§ãã¾ãã
jbang https://gist.github.com/cescoffier/e8c8a18897f9e5ca15f1378876a1bd93
merge ã concatenate ã«ç½®ãæããã¨éãããããã¾ãã
楽ãããï¼