Skip to content

Commit

Permalink
use JedisPoolUser more
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-besga-panel committed Aug 31, 2023
1 parent 6ff6b5f commit 2f8cbc2
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 105 deletions.
74 changes: 30 additions & 44 deletions src/main/java/org/oba/jedis/extra/utils/cache/SimpleCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.oba.jedis.extra.utils.cache;

import org.oba.jedis.extra.utils.utils.JedisPoolUser;
import org.oba.jedis.extra.utils.utils.Listable;
import org.oba.jedis.extra.utils.utils.Mapeable;
import org.oba.jedis.extra.utils.utils.Named;
Expand Down Expand Up @@ -46,7 +47,7 @@
* And a timeout that will be applied to all data by default
*/
public class SimpleCache implements Iterable<Map.Entry<String,String>>,
Listable<Map.Entry<String,String>>, Mapeable<String, String>, Named {
Listable<Map.Entry<String,String>>, Mapeable<String, String>, Named, JedisPoolUser {

private static final Logger LOGGER = LoggerFactory.getLogger(SimpleCache.class);

Expand Down Expand Up @@ -109,11 +110,8 @@ public SimpleCache withCacheWriter(CacheWriter cacheWriter) {
return this;
}

/**
* Return the current jedis pol
* @return jedis pool
*/
JedisPool getJedisPool() {
@Override
public JedisPool getJedisPool() {
return jedisPool;
}

Expand Down Expand Up @@ -167,13 +165,11 @@ public String get(String key) {
public String get(String key, CacheLoader cacheLoader) {
checkClosed();
if (key == null) throw new IllegalArgumentException("RedisCache.get key is null");
try (Jedis jedis = jedisPool.getResource()) {
String value = jedis.get(resolveKey(key));
if (value == null) {
value = readThrougth(jedis, key, cacheLoader);
}
return value;
String value = withJedisPoolGet(jedis -> jedis.get(resolveKey(key)));
if (value == null) {
value = withJedisPoolGet(jedis -> readThrougth(jedis, key, cacheLoader));
}
return value;
}

/**
Expand Down Expand Up @@ -260,9 +256,7 @@ private String readThrougth(Jedis jedis, String key, CacheLoader cacheLoader) {
*/
public boolean containsKey(String key) {
checkClosed();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.exists(resolveKey(key));
}
return withJedisPoolGet(jedis -> jedis.exists(resolveKey(key)));
}

/**
Expand Down Expand Up @@ -314,13 +308,11 @@ public void put(String key, String value, long timeOutMs) {
checkClosed();
if (key == null) throw new IllegalArgumentException("RedisCache.put key is null");
if (value == null) throw new IllegalArgumentException("RedisCache.put value is null");
try (Jedis jedis = jedisPool.getResource()) {
SetParams setParams = new SetParams().px(timeOutMs);
jedis.set(resolveKey(key), value, setParams);
if (cacheWriter != null) {
LOGGER.debug("write-through store key {} value {}", key, value);
cacheWriter.write(key, value);
}
SetParams setParams = new SetParams().px(timeOutMs);
withJedisPoolDo(jedis -> jedis.set(resolveKey(key), value, setParams));
if (cacheWriter != null) {
LOGGER.debug("write-through store key {} value {}", key, value);
cacheWriter.write(key, value);
}
}

Expand Down Expand Up @@ -399,15 +391,13 @@ public boolean putIfAbsent(String key, String value) {
checkClosed();
if (key == null) throw new IllegalArgumentException("RedisCache.putIfAbsent key is null");
if (value == null) throw new IllegalArgumentException("RedisCache.putIfAbsent value is null");
try (Jedis jedis = jedisPool.getResource()) {
SetParams setParams = new SetParams().nx().px(timeOutMs);
String result = jedis.set(resolveKey(key), value, setParams );
if (result!= null && cacheWriter != null) {
LOGGER.debug("write-through store key {} value {}", key, value);
cacheWriter.write(key, value);
}
return result != null;
SetParams setParams = new SetParams().nx().px(timeOutMs);
String result = withJedisPoolGet(jedis -> jedis.set(resolveKey(key), value, setParams));
if (result!= null && cacheWriter != null) {
LOGGER.debug("write-through store key {} value {}", key, value);
cacheWriter.write(key, value);
}
return result != null;
}

/**
Expand Down Expand Up @@ -447,18 +437,16 @@ public boolean remove(String key, String oldValue) {
//Better with script
if (key == null) throw new IllegalArgumentException("RedisCache.remove key is null");
if (oldValue == null) throw new IllegalArgumentException("RedisCache.remove oldValue is null");
try (Jedis jedis = jedisPool.getResource()) {
String current = jedis.get(resolveKey(key));
if (current != null && current.equals(oldValue)) {
jedis.del(resolveKey(key));
if (cacheWriter != null) {
LOGGER.debug("write-through remove key {} ", key);
cacheWriter.delete(key);
}
return true;
} else {
return false;
String current = withJedisPoolGet(jedis -> jedis.get(resolveKey(key)));
if (current != null && current.equals(oldValue)) {
withJedisPoolDo(jedis -> jedis.del(resolveKey(key)));
if (cacheWriter != null) {
LOGGER.debug("write-through remove key {} ", key);
cacheWriter.delete(key);
}
return true;
} else {
return false;
}
}

Expand Down Expand Up @@ -628,9 +616,7 @@ private void removeAll(boolean allowCacheWriter) {
List<String> scanned = new CacheKeyIterator(this, false).asList();
// No need to convert here
if (!scanned.isEmpty()) {
try (Jedis jedis = jedisPool.getResource()) {
jedis.del(scanned.toArray(new String[]{}));
}
withJedisPoolDo(jedis -> jedis.del(scanned.toArray(new String[]{})));
if (allowCacheWriter && cacheWriter != null) {
List<String> unresolved = scanned.stream().
map(this::unresolveKey).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.oba.jedis.extra.utils.countdownlatch;

import org.oba.jedis.extra.utils.utils.JedisPoolUser;
import org.oba.jedis.extra.utils.utils.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -22,7 +23,7 @@
*
*
*/
public class JedisCountDownLatch implements Named {
public class JedisCountDownLatch implements Named, JedisPoolUser {

private static final Logger LOGGER = LoggerFactory.getLogger(JedisCountDownLatch.class);
private static final Long LONG_NULL_VALUE = -1L;
Expand Down Expand Up @@ -74,6 +75,11 @@ public String getName() {
return name;
}

@Override
public JedisPool getJedisPool() {
return jedisPool;
}

/**
* Wait until interrupted or shared value reaches zero
* @throws InterruptedException if interrupted
Expand Down Expand Up @@ -107,11 +113,9 @@ public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
* @return the current value, after operation
*/
public long countDown() {
try (Jedis jedis = jedisPool.getResource()) {
long value = jedis.decr(name);
LOGGER.debug("countDown name {} value {}", name, value);
return value;
}
long value = withJedisPoolGet(jedis -> jedis.decr(name));
LOGGER.debug("countDown name {} value {}", name, value);
return value;
}

private boolean isCountZero() {
Expand All @@ -123,14 +127,12 @@ private boolean isCountZero() {
* @return current value
*/
public long getCount() {
try (Jedis jedis = jedisPool.getResource()) {
String value = jedis.get(name);
LOGGER.debug("getCount name {} value {}", name, value);
if (value != null && !value.isEmpty()) {
return Long.parseLong(value);
} else {
return LONG_NULL_VALUE;
}
String value = withJedisPoolGet(jedis -> jedis.get(name));
LOGGER.debug("getCount name {} value {}", name, value);
if (value != null && !value.isEmpty()) {
return Long.parseLong(value);
} else {
return LONG_NULL_VALUE;
}
}

Expand All @@ -140,9 +142,7 @@ public long getCount() {
* USE AT YOUR OWN RISK WHEN ALL POSSIBLE OPERATIONS ARE FINISHED
*/
public void destroy() {
try (Jedis jedis = jedisPool.getResource()) {
jedis.del(name);
}
withJedisPoolDo(jedis -> jedis.del(name));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public abstract class AbstractInterruptingJedisLock implements IJedisLock {
this.timeUnit = timeUnit;
}

@Override
public String getName() {
return jedisLock.getName();
}

@Override
public JedisPool getJedisPool() {
return jedisLock.getJedisPool();
}

public boolean isLocked() {
return jedisLock.isLocked();
}
Expand Down Expand Up @@ -104,11 +114,6 @@ public TimeUnit getTimeUnit() {
return this.timeUnit;
}

@Override
public String getName() {
return jedisLock.getName();
}

@Override
public long getLeaseMoment() {
return jedisLock.getLeaseMoment();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.oba.jedis.extra.utils.interruptinglocks;


import org.oba.jedis.extra.utils.utils.JedisPoolUser;
import org.oba.jedis.extra.utils.utils.Named;

import java.util.concurrent.TimeUnit;
Expand All @@ -9,7 +10,7 @@
/**
* Interface for locks on redis
*/
public interface IJedisLock extends AutoCloseable, Named {
public interface IJedisLock extends AutoCloseable, Named, JedisPoolUser {

/**
* Lease time of the lock, null if none
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.oba.jedis.extra.utils.interruptinglocks;

import org.oba.jedis.extra.utils.utils.JedisPoolUser;
import org.oba.jedis.extra.utils.utils.ScriptEvalSha1;
import org.oba.jedis.extra.utils.utils.UniversalReader;
import org.slf4j.Logger;
Expand All @@ -15,7 +16,6 @@
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand All @@ -29,7 +29,7 @@
* https://redis.io/topics/distlock
*
*/
public class JedisLock implements IJedisLock {
public class JedisLock implements IJedisLock, JedisPoolUser {

private static final Logger LOGGER = LoggerFactory.getLogger(JedisLock.class);

Expand Down Expand Up @@ -97,6 +97,11 @@ private synchronized static String generateUniqueTokenValue(String name){
return name + "_" + System.currentTimeMillis() + "_" + ThreadLocalRandom.current().nextInt(1_000_000);
}

@Override
public JedisPool getJedisPool() {
return jedisPool;
}

public void setWaitCylce(int time, TimeUnit timeUnit){
this.waitCylce = timeUnit.toMillis(time);
}
Expand All @@ -117,14 +122,6 @@ public String getName() {
return name;
}

/**
* JedisPool object used to lock
* @return jedisPool
*/
public JedisPool getJedisPool() {
return jedisPool;
}

@Override
public long getLeaseMoment() {
return leaseMoment;
Expand Down Expand Up @@ -199,11 +196,6 @@ public synchronized <T> T underLock(Supplier<T> task) {
}
}

private <T> T underPool(Function<Jedis, T> action) {
try (Jedis jedis = jedisPool.getResource()) {
return action.apply(jedis);
}
}

/**
* Attempts to get the lock.
Expand All @@ -212,7 +204,7 @@ private <T> T underPool(Function<Jedis, T> action) {
* @return true if lock obtained, false otherwise
*/
private synchronized boolean redisLock() {
return underPool(this::redisLockUnderPool);
return withJedisPoolGet(this::redisLockUnderPool);
}

private synchronized boolean redisLockUnderPool(Jedis jedis) {
Expand Down Expand Up @@ -265,7 +257,7 @@ private synchronized void redisUnlock() {
* @return true if the lock is remotely held
*/
private synchronized boolean redisCheckLock() {
return underPool(this::redisCheckLockUnderPool);
return withJedisPoolGet(this::redisCheckLockUnderPool);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.oba.jedis.extra.utils.interruptinglocks;


import org.oba.jedis.extra.utils.utils.JedisPoolUser;
import org.oba.jedis.extra.utils.utils.Named;
import redis.clients.jedis.JedisPool;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
Expand All @@ -11,7 +13,7 @@
/**
* java.util.concurrent.locks.Lock from a Redis Lock
*/
public class LockFromRedis implements Lock, AutoCloseable, Named {
public class LockFromRedis implements Lock, AutoCloseable, Named, JedisPoolUser {


private final IJedisLock jedisLock;
Expand All @@ -29,6 +31,12 @@ public String getName() {
return jedisLock.getName();
}

@Override
public JedisPool getJedisPool() {
return jedisLock.getJedisPool();
}


@Override
public void lock() {
jedisLock.lock();
Expand Down
Loading

0 comments on commit 2f8cbc2

Please sign in to comment.