瀏覽代碼

add set_plaintext, remove ActivationCodeService

tags/2.0.1
Jonathan Cobb 4 年之前
父節點
當前提交
23e6ab7399
共有 2 個文件被更改,包括 35 次插入81 次删除
  1. +0
    -73
      wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/ActivationCodeService.java
  2. +35
    -8
      wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/RedisService.java

+ 0
- 73
wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/ActivationCodeService.java 查看文件

@@ -1,73 +0,0 @@
package org.cobbzilla.wizard.cache.redis;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.cobbzilla.util.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.cobbzilla.wizard.cache.redis.RedisService.EX;
import static org.cobbzilla.wizard.cache.redis.RedisService.NX;

@Service @Slf4j
public class ActivationCodeService {

@Autowired @Getter @Setter private RedisService redis;

public String peek(String key) { return redis.get_plaintext(key); }

public boolean attempt(String key, String claimant) {
try {
final Long remaining = redis.decr(key);
if (remaining == null || remaining < 0) return false;
redis.lpush(getClaimantsKey(key), claimant);
return true;

} catch (Exception e) {
log.warn("attempt("+key+") error: "+e);
return false;
}
}

public void define (String key, int quantity, long expirationSeconds) {
redis.set_plaintext(key, String.valueOf(quantity), NX, EX, expirationSeconds);
}

public List<String> getClaimants (String key) { return redis.list(getClaimantsKey(key)); }

private String getClaimantsKey(String key) { return key+"_claimed"; }

/**
* @param args [0] = key; [1] = quantity; [2] = expiration (# days); [3] = redis key (optional)
*/
public static void main (final String[] args) {

final RedisService redis = new RedisService();
final String redisKey = (args.length == 4) ? args[3] : null;

redis.setConfiguration(() -> new RedisConfiguration(redisKey));

final ActivationCodeService acService = new ActivationCodeService();
acService.setRedis(redis);

final String key = args[0];

if (args.length > 1) {
final int quantity = Integer.parseInt(args[1]);
final long expirationSeconds = Integer.parseInt(args[2]) * TimeUnit.DAYS.toSeconds(1);

acService.define(key, quantity, expirationSeconds);
System.out.println("successfully defined key: " + key);

} else {
System.out.println("key: " + key);
System.out.println("remaining: " + acService.peek(key));
System.out.println("claimants: " + StringUtil.toString(acService.getClaimants(key), ", "));
}
}

}

+ 35
- 8
wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/RedisService.java 查看文件

@@ -130,6 +130,17 @@ public class RedisService {

public void set(String key, String value) { __set(key, value, 0, MAX_RETRIES); }

public void set_plaintext(String key, String value, String nxxx, String expx, long time) {
__set_plaintext(key, value, nxxx, expx, time, 0, MAX_RETRIES);
}

public void set_plaintext(String key, String value, String expx, long time) {
__set_plaintext(key, value, XX, expx, time, 0, MAX_RETRIES);
__set_plaintext(key, value, NX, expx, time, 0, MAX_RETRIES);
}

public void set_plaintext(String key, String value) { __set_plaintext(key, value, 0, MAX_RETRIES); }

public void setAll(Collection<String> keys, String value, String expx, long time) {
for (String k : keys) set(k, value, expx, time);
}
@@ -164,14 +175,6 @@ public class RedisService {
public Long del(String key) { return __del(key, 0, MAX_RETRIES); }
public Long del_withPrefix(String prefixedKey) { return __del(prefixedKey, 0, MAX_RETRIES, false); }

public void set_plaintext(String key, String value, String nxxx, String expx, long time) {
__set(key, value, nxxx, expx, time, 0, MAX_RETRIES);
}

public void set_plaintext(String key, String value) {
__set(key, value, 0, MAX_RETRIES);
}

public Long sadd(String key, String value) { return sadd(key, new String[]{value}); }
public Long sadd(String key, String[] values) { return __sadd(key, values, 0, MAX_RETRIES); }

@@ -372,6 +375,30 @@ public class RedisService {
}
}

private String __set_plaintext(String key, String value, String nxxx, String expx, long time, int attempt, int maxRetries) {
try {
synchronized (redis) {
return getRedis().set(prefix(key), value, nxxx, expx, time);
}
} catch (RuntimeException e) {
if (attempt > maxRetries) throw e;
resetForRetry(attempt, "retrying RedisService.__set_plaintext");
return __set_plaintext(key, value, nxxx, expx, time, attempt + 1, maxRetries);
}
}

private String __set_plaintext(String key, String value, int attempt, int maxRetries) {
try {
synchronized (redis) {
return getRedis().set(prefix(key), value);
}
} catch (RuntimeException e) {
if (attempt > maxRetries) throw e;
resetForRetry(attempt, "retrying RedisService.__set_plaintext");
return __set_plaintext(key, value, attempt+1, maxRetries);
}
}

private Long __lpush(String key, String value, int attempt, int maxRetries) {
try {
synchronized (redis) {


Loading…
取消
儲存