All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.sinri.keel.redis.RedisKit Maven / Gradle / Ivy

Go to download

A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.

The newest version!
package io.github.sinri.keel.redis;

import io.github.sinri.keel.redis.mixin.*;
import io.vertx.core.Future;
import io.vertx.redis.client.Redis;
import io.vertx.redis.client.RedisAPI;
import io.vertx.redis.client.RedisConnection;
import io.vertx.redis.client.RedisOptions;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

import static io.github.sinri.keel.facade.KeelInstance.Keel;

/**
 * @since 3.0.5
 */
public class RedisKit implements RedisApiMixin, RedisScalarMixin, RedisListMixin, RedisBitMixin, RedisHashMixin, RedisSetMixin, RedisOrderedSetMixin {
    private final Redis client;
    private final AtomicReference redisConnectionRef = new AtomicReference<>();

    public RedisKit(String redisInstanceKey) {
        /*
         * URL should be redis://[:password@]host[:port][/db-number]
         */
        String url = Keel.getConfiguration().readString(List.of("redis", redisInstanceKey, "url"), null);
        Objects.requireNonNull(url);
        this.client = Redis.createClient(Keel.getVertx(), new RedisOptions()
                .setConnectionString(url)
                .setMaxPoolSize(16)
                .setMaxWaitingHandlers(32)
                .setMaxPoolWaiting(24)
                .setPoolCleanerInterval(5000)
        );
    }

    public Redis getClient() {
        return client;
    }

    @Override
    public Future api() {
        // since 20230901, try to resolve pool max over issue
        if (redisConnectionRef.get() == null) {
            return getClient().connect()
                    .compose(redisConnection -> {
                        redisConnectionRef.set(redisConnection);
                        return Future.succeededFuture(RedisAPI.api(redisConnectionRef.get()));
                    });
        } else {
            return Future.succeededFuture(RedisAPI.api(redisConnectionRef.get()));
        }
    }

    /**
     * @since 3.2.18
     */
    public void close() {
        this.client.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy