2022-10-29 20:02:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-present RedisBungee contributors
|
|
|
|
*
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
*
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*/
|
|
|
|
|
2022-07-16 12:50:09 +00:00
|
|
|
package com.imaginarycode.minecraft.redisbungee.api.summoners;
|
2022-04-14 10:12:47 +00:00
|
|
|
|
|
|
|
import redis.clients.jedis.Jedis;
|
2022-04-14 10:34:56 +00:00
|
|
|
import redis.clients.jedis.JedisPool;
|
2022-07-26 13:47:18 +00:00
|
|
|
import redis.clients.jedis.JedisPooled;
|
2022-10-25 04:01:41 +00:00
|
|
|
import redis.clients.jedis.providers.PooledConnectionProvider;
|
2022-04-14 10:12:47 +00:00
|
|
|
|
2022-07-16 05:18:33 +00:00
|
|
|
import java.io.IOException;
|
2022-07-06 19:04:09 +00:00
|
|
|
|
2022-07-26 13:47:18 +00:00
|
|
|
public class JedisPooledSummoner implements Summoner<JedisPooled> {
|
2022-04-14 10:12:47 +00:00
|
|
|
|
2022-10-25 04:01:41 +00:00
|
|
|
private final PooledConnectionProvider connectionProvider;
|
2022-07-16 05:18:33 +00:00
|
|
|
private final JedisPool jedisPool;
|
2022-04-14 10:12:47 +00:00
|
|
|
|
2022-10-25 04:01:41 +00:00
|
|
|
public JedisPooledSummoner(PooledConnectionProvider connectionProvider, JedisPool jedisPool) {
|
|
|
|
this.connectionProvider = connectionProvider;
|
2022-07-16 05:18:33 +00:00
|
|
|
this.jedisPool = jedisPool;
|
2022-07-26 13:55:16 +00:00
|
|
|
// test connections
|
|
|
|
if (jedisPool != null) {
|
|
|
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
|
|
|
// Test the connection to make sure configuration is right
|
|
|
|
jedis.ping();
|
|
|
|
}
|
2022-10-25 04:01:41 +00:00
|
|
|
|
2022-07-17 01:25:50 +00:00
|
|
|
}
|
2022-10-25 04:01:41 +00:00
|
|
|
final JedisPooled jedisPooled = this.obtainResource();
|
2022-07-26 13:55:16 +00:00
|
|
|
jedisPooled.set("random_data", "0");
|
|
|
|
jedisPooled.del("random_data");
|
|
|
|
|
2022-07-16 05:18:33 +00:00
|
|
|
}
|
2022-04-14 10:12:47 +00:00
|
|
|
|
2022-07-16 05:18:33 +00:00
|
|
|
@Override
|
2022-07-26 13:47:18 +00:00
|
|
|
public JedisPooled obtainResource() {
|
2022-10-25 04:01:41 +00:00
|
|
|
// create UnClosable JedisPool *disposable*
|
|
|
|
return new NotClosableJedisPooled(this.connectionProvider);
|
2022-07-16 05:18:33 +00:00
|
|
|
}
|
2022-04-14 10:12:47 +00:00
|
|
|
|
2022-07-26 13:47:18 +00:00
|
|
|
public JedisPool getCompatibilityJedisPool() {
|
2022-07-16 05:18:33 +00:00
|
|
|
return this.jedisPool;
|
|
|
|
}
|
2022-07-15 22:58:48 +00:00
|
|
|
|
2022-07-16 05:18:33 +00:00
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
2022-10-17 04:27:55 +00:00
|
|
|
if (this.jedisPool != null) {
|
|
|
|
this.jedisPool.close();
|
|
|
|
}
|
2022-10-25 04:01:41 +00:00
|
|
|
this.connectionProvider.close();
|
2022-07-16 05:18:33 +00:00
|
|
|
|
|
|
|
}
|
2022-04-14 10:12:47 +00:00
|
|
|
}
|