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

com.github.enesusta.redis.PoolRedisDataSource Maven / Gradle / Ivy

package com.github.enesusta.redis;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

public class PoolRedisDataSource implements RedisDataSource {

    private final RedisConfiguration redisConfiguration;

    public PoolRedisDataSource(final RedisConfiguration redisConfiguration) {
        this.redisConfiguration = redisConfiguration;
    }

    @Override
    public JedisPool getRedisDataSource() {

        final JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(128);
        poolConfig.setMaxIdle(128);
        poolConfig.setMinIdle(16);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        poolConfig.setTestWhileIdle(true);
        poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
        poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
        poolConfig.setNumTestsPerEvictionRun(3);
        poolConfig.setBlockWhenExhausted(true);

        final String host = redisConfiguration.getHost();
        final int port = redisConfiguration.getPort();
        final int timeout = redisConfiguration.getTimeout();
        final String password = redisConfiguration.getPassword();

        final JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout, password);

        return jedisPool;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy