Skip to content

Commit

Permalink
some little updates
Browse files Browse the repository at this point in the history
sempaphore with jedis pool
  • Loading branch information
oscar-besga-panel committed Aug 30, 2023
1 parent db3d32d commit 6ff6b5f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ services:
redis:
image: 'redis:5.0.13'
hostname: redis
container_name: redis
container_name: redis5013
ports:
- '6379:6379'
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.oba.jedis.extra.utils.semaphore;


import org.oba.jedis.extra.utils.utils.Named;
import org.oba.jedis.extra.utils.utils.ScriptEvalSha1;
import org.oba.jedis.extra.utils.utils.ScriptHolder;
import org.oba.jedis.extra.utils.utils.UniversalReader;
import redis.clients.jedis.Jedis;
import org.oba.jedis.extra.utils.utils.*;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

Expand Down Expand Up @@ -34,7 +30,7 @@
*
*
*/
public class JedisSemaphore implements Named {
public class JedisSemaphore implements Named, JedisPoolUser {

public static final String SCRIPT_NAME = "semaphore.lua";
public static final String FILE_PATH = "./src/main/resources/semaphore.lua";
Expand Down Expand Up @@ -110,11 +106,15 @@ private void init(int initialPermits){
if (initialPermits < 0) {
throw new IllegalArgumentException("initial permit on semaphore must be always equal or more than zero");
}
try (Jedis jedis = jedisPool.getResource()) {
jedis.set(name, String.valueOf(initialPermits), new SetParams().nx());
}
withJedisPoolDo(jedis -> jedis.set(name, String.valueOf(initialPermits), new SetParams().nx()));
}

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


/**
* Returns the sempahore name
* @return name
Expand Down Expand Up @@ -215,9 +215,7 @@ public void release(int permits) {
if (permits <= 0) {
throw new IllegalArgumentException("permit to release on semaphore must be always more than zero");
}
try (Jedis jedis = jedisPool.getResource()) {
jedis.incrBy(name, permits);
}
withJedisPoolDo(jedis -> jedis.incrBy(name, permits));
}

/**
Expand All @@ -226,27 +224,21 @@ public void release(int permits) {
* @return number of permits
*/
public int availablePermits() {
String permits;
try (Jedis jedis = jedisPool.getResource()) {
permits = jedis.get(name);
}
String permits = withJedisPoolGet(jedis -> jedis.get(name));
if (permits == null || permits.isEmpty()) {
return -1;
} else {
return Integer.parseInt(permits);
}
}


/**
* CAUTION !!
* THIS METHOD DELETES THE REMOTE VALUE DESTROYING THIS SEMAPHORE AND OHTERS
* USE AT YOUR OWN RISK WHEN ALL POSSIBLE OPERATIONS ARE FINISHED
*/
public void destroy(){
try (Jedis jedis = jedisPool.getResource()) {
jedis.del(name);
}
public void destroy() {
withJedisPoolDo(jedis -> jedis.del(name));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ public class ScriptEvalSha1 implements JedisPoolUser {
private String sha1Digest;

public ScriptEvalSha1(JedisPool jedisPool, UniversalReader scriptSource) {
this(jedisPool, scriptSource, false);
}

public ScriptEvalSha1(JedisPool jedisPool, UniversalReader scriptSource, boolean autoload) {
this.jedisPool = jedisPool;
this.scriptSource = scriptSource;
if (autoload) {
load();
}
}

public String getSha1Digest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void addScriptWithResourceAndFile(String key, String resource, String fil
UniversalReader reader = new UniversalReader().
withResoruce(resource).
withFile(file);
ScriptEvalSha1 script = new ScriptEvalSha1(jedisPool, reader);
script.load();
ScriptEvalSha1 script = new ScriptEvalSha1(jedisPool, reader, true);
scriptMap.put(key, script);
}

Expand Down

0 comments on commit 6ff6b5f

Please sign in to comment.