Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

preface

If you want to use Quarkus native Image for local compilation, the use of Spring and other Redisclient may lead to some problems that are difficult to solve. In place of quarkus-Redis-client, this article will briefly discuss how to use quarkus-Redis-client as a caching method. In this article, we will implement the simple operation methods of GET, SET, DEL and KEYS on the Redis client, and add caching to the weather forecast responsiveness method we mentioned above.

Configuration and Dependencies

Some of the dependencies related to Quarkus have been shown in the previous article. Quarkus-redis-client is introduced in Maven

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-redis-client</artifactId>
</dependency>
Copy the code

Then add the redis configuration to the configured properties

quarkus.redis.hosts=redis://localhost:6379
quarkus.redis.database=0
quarkus.redis.timeout=10s
Copy the code

Then write redisService to make use of more traditional Redis client services.

@ApplicationScoped public class RedisClientService { @Inject RedisClient redisClient; @Inject ReactiveRedisClient reactiveRedisClient; public String get(String key) { Response response = redisClient.get(key); return response == null ? null : response.toString(); } void set(String key, String value) { redisClient.set(Arrays.asList(key, value.toString())); } Uni<JsonArray> result = // Step 1 - Obtain key reactiveredisclient.keys ("*").onitem (). TransformToMulti (keys -> Multi-.createfrom ().iterable(keys)).onitem ().castto (string.class) // Step 2 - Retrieve related objects for each key OnItem (.) transformToUniAndMerge (key - > reactiveRedisClient. Hgetall (key)) / / Step 3 and 4 - to generate JsonArray containing all of the objects .collect().in(JsonArray::new, JsonArray::add);Copy the code

There are two types of clients in Quarkus’ Redis client, one is RedisClient and the other is ReactiveRedisClient. RedisClient supports traditional add, delete, change and check. ReactiveRedisClient is a more responsive programming method written by Quarkus. For example, if you want to implement a redis usage:

1. Get all keys from Redis 2. For each key -> retrieve the associated object 3. Add this object to the JsonArray. 4. Generate a JsonArray containing all objectsCopy the code

How to achieve this effect easily and effectively without adding a lot of business code. The Uni returned directly from the ReactiveRedisClient will do:

Uni<JsonArray> result = // Step 1 - Obtain key reactiveredisclient.keys ("*").onitem (). TransformToMulti (keys -> Multi-.createfrom ().iterable(keys)).onitem ().castto (string.class) // Step 2 - Retrieve related objects for each key .onitem ().transformtouniandMerge (key -> redis.hgetall(key)) // Step 3 and 4 - Generate jsonArray.collect (). In (() -> new  JsonArray(), (arr, obj) -> arr.add(obj));Copy the code

The responsive code design in Quarkus greatly simplifies the implementation of many business logic and calculations.

Here we use the original code logic to fetch the weather and cache the weather in the Redis client

public Uni<Weather> testCache( String name) { Weather dto = null; if(null! =redisClientService.get(name)){ dto= JSONObject.parseObject(redisClientService.get(name), Weather.class); return Uni.createFrom().item(dto); } String code= cityService.getCode(name); try { dto = JSONObject.parseObject(restClient.getStream(code), Weather.class); } catch (IOException e) { e.printStackTrace(); } redisClientService.set(name,JSONObject.toJSONString(dto)); return Uni.createFrom().item(dto); }Copy the code

Brew Services Start Redis is used to start the redis server.

use$ mvn quarkus:devStart debugging mode, the first execution is no cache and calls the network interface directly

And then calling it again is many times faster

To make a simple comparison, call 150 times at the same time, different cities, cache and interface speed comparison, first use interface:

Private static String[] cityNames = new String[]{" cityNames ", "cityNames"; private static int[] cityTimes = new int[]{50, 40, 30, 20, 10};Copy the code

The result of the call is

A total of 10685 ms

Versions that use caching:

A total of 1478 ms

Considering various factors, caching speeds up the entire query process by about 10 times.

reference

Quarkus. IO/guides/redi… Quarkus. IO/blog/mutiny…