Kaynağa Gözat

use redis constants

tags/2.0.1
Jonathan Cobb 4 yıl önce
ebeveyn
işleme
e3256afba7
6 değiştirilmiş dosya ile 27 ekleme ve 14 silme
  1. +4
    -1
      wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/ActivationCodeService.java
  2. +4
    -2
      wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/RedisMap.java
  3. +7
    -3
      wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/RedisService.java
  4. +6
    -4
      wizard-server/src/main/java/org/cobbzilla/wizard/dao/AbstractRedisDAO.java
  5. +2
    -1
      wizard-server/src/main/java/org/cobbzilla/wizard/dao/AbstractSessionDAO.java
  6. +4
    -3
      wizard-server/src/main/java/org/cobbzilla/wizard/dao/shard/cache/ShardCacheableFinder.java

+ 4
- 1
wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/ActivationCodeService.java Dosyayı Görüntüle

@@ -10,6 +10,9 @@ import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; 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 @Service @Slf4j
public class ActivationCodeService { public class ActivationCodeService {


@@ -31,7 +34,7 @@ public class ActivationCodeService {
} }


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


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


+ 4
- 2
wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/RedisMap.java Dosyayı Görüntüle

@@ -13,6 +13,8 @@ import static org.cobbzilla.util.daemon.ZillaRuntime.notSupported;
import static org.cobbzilla.util.json.JsonUtil.fromJsonOrDie; import static org.cobbzilla.util.json.JsonUtil.fromJsonOrDie;
import static org.cobbzilla.util.json.JsonUtil.toJsonOrDie; import static org.cobbzilla.util.json.JsonUtil.toJsonOrDie;
import static org.cobbzilla.util.reflect.ReflectionUtil.getFirstTypeParam; import static org.cobbzilla.util.reflect.ReflectionUtil.getFirstTypeParam;
import static org.cobbzilla.wizard.cache.redis.RedisService.NX;
import static org.cobbzilla.wizard.cache.redis.RedisService.XX;


@AllArgsConstructor @AllArgsConstructor
public class RedisMap<V> implements Map<String, V> { public class RedisMap<V> implements Map<String, V> {
@@ -46,8 +48,8 @@ public class RedisMap<V> implements Map<String, V> {
} else if (duration == null) { } else if (duration == null) {
redis.set(keyName(key), toJsonOrDie(value)); redis.set(keyName(key), toJsonOrDie(value));
} else { } else {
redis.set(keyName(key), toJsonOrDie(value), "NX", "PX", duration);
redis.set(keyName(key), toJsonOrDie(value), "XX", "PX", duration);
redis.set(keyName(key), toJsonOrDie(value), NX, "PX", duration);
redis.set(keyName(key), toJsonOrDie(value), XX, "PX", duration);
} }
return null; return null;
} }


+ 7
- 3
wizard-server/src/main/java/org/cobbzilla/wizard/cache/redis/RedisService.java Dosyayı Görüntüle

@@ -27,6 +27,10 @@ public class RedisService {


public static final int MAX_RETRIES = 5; public static final int MAX_RETRIES = 5;


public static final String EX = "EX";
public static final String XX = "XX";
public static final String NX = "NX";

@Autowired @Getter @Setter private HasRedisConfiguration configuration; @Autowired @Getter @Setter private HasRedisConfiguration configuration;


@Getter @Setter private String key; @Getter @Setter private String key;
@@ -118,8 +122,8 @@ public class RedisService {
} }


public void set(String key, String value, String expx, long time) { public void set(String key, String value, String expx, long time) {
__set(key, value, "XX", expx, time, 0, MAX_RETRIES);
__set(key, value, "NX", expx, time, 0, MAX_RETRIES);
__set(key, value, XX, expx, time, 0, MAX_RETRIES);
__set(key, value, NX, expx, time, 0, MAX_RETRIES);
} }


public void set(String key, String value) { __set(key, value, 0, MAX_RETRIES); } public void set(String key, String value) { __set(key, value, 0, MAX_RETRIES); }
@@ -215,7 +219,7 @@ public class RedisService {
String lockVal = get(key); String lockVal = get(key);
final long start = now(); final long start = now();
while ((lockVal == null || !lockVal.equals(uuid)) && (now() - start < lockTimeout)) { while ((lockVal == null || !lockVal.equals(uuid)) && (now() - start < lockTimeout)) {
set(key, uuid, "NX", "EX", deadlockTimeout);
set(key, uuid, NX, EX, deadlockTimeout);
lockVal = get(key); lockVal = get(key);
} }
if (lockVal == null || !lockVal.equals(uuid)) return die("lock: timeout locking "+key); if (lockVal == null || !lockVal.equals(uuid)) return die("lock: timeout locking "+key);


+ 6
- 4
wizard-server/src/main/java/org/cobbzilla/wizard/dao/AbstractRedisDAO.java Dosyayı Görüntüle

@@ -15,6 +15,7 @@ import static org.cobbzilla.util.daemon.ZillaRuntime.notSupported;
import static org.cobbzilla.util.json.JsonUtil.fromJsonOrDie; import static org.cobbzilla.util.json.JsonUtil.fromJsonOrDie;
import static org.cobbzilla.util.json.JsonUtil.toJsonOrDie; import static org.cobbzilla.util.json.JsonUtil.toJsonOrDie;
import static org.cobbzilla.util.reflect.ReflectionUtil.getTypeParameter; import static org.cobbzilla.util.reflect.ReflectionUtil.getTypeParameter;
import static org.cobbzilla.wizard.cache.redis.RedisService.*;


public abstract class AbstractRedisDAO<E extends ExpirableBase> implements DAO<E> { public abstract class AbstractRedisDAO<E extends ExpirableBase> implements DAO<E> {


@@ -63,8 +64,8 @@ public abstract class AbstractRedisDAO<E extends ExpirableBase> implements DAO<E


@Override public E update(@Valid E entity) { @Override public E update(@Valid E entity) {
if (entity.shouldExpire()) { if (entity.shouldExpire()) {
getRedis().set(entity.getUuid(), toJsonOrDie(entity), "XX", "EX", entity.getExpirationSeconds());
getRedis().set(entity.getUuid(), toJsonOrDie(entity), "NX", "EX", entity.getExpirationSeconds());
getRedis().set(entity.getUuid(), toJsonOrDie(entity), XX, EX, entity.getExpirationSeconds());
getRedis().set(entity.getUuid(), toJsonOrDie(entity), NX, EX, entity.getExpirationSeconds());
} else { } else {
getRedis().set(entity.getUuid(), toJsonOrDie(entity)); getRedis().set(entity.getUuid(), toJsonOrDie(entity));
} }
@@ -80,7 +81,8 @@ public abstract class AbstractRedisDAO<E extends ExpirableBase> implements DAO<E
for (E entity : entities) getRedis().del(entity.getUuid()); for (E entity : entities) getRedis().del(entity.getUuid());
} }


public String getMetadata (String key) { return getRedis().get("__metadata_"+key); }
public void setMetadata (String key, String value) { getRedis().set("__metadata_"+key, value); }
public static final String METADATA_PREFIX = "__metadata_";
public String getMetadata (String key) { return getRedis().get(METADATA_PREFIX+key); }
public void setMetadata (String key, String value) { getRedis().set(METADATA_PREFIX+key, value); }


} }

+ 2
- 1
wizard-server/src/main/java/org/cobbzilla/wizard/dao/AbstractSessionDAO.java Dosyayı Görüntüle

@@ -12,6 +12,7 @@ import static java.util.UUID.randomUUID;
import static java.util.concurrent.TimeUnit.DAYS; import static java.util.concurrent.TimeUnit.DAYS;
import static org.cobbzilla.util.daemon.ZillaRuntime.empty; import static org.cobbzilla.util.daemon.ZillaRuntime.empty;
import static org.cobbzilla.util.reflect.ReflectionUtil.getFirstTypeParam; import static org.cobbzilla.util.reflect.ReflectionUtil.getFirstTypeParam;
import static org.cobbzilla.wizard.cache.redis.RedisService.*;
import static org.cobbzilla.wizard.resources.ResourceUtil.forbiddenEx; import static org.cobbzilla.wizard.resources.ResourceUtil.forbiddenEx;


@Slf4j @Slf4j
@@ -62,7 +63,7 @@ public abstract class AbstractSessionDAO<T extends Identifiable> {
} }


private void rawSet(String uuid, T thing, boolean shouldExist) { private void rawSet(String uuid, T thing, boolean shouldExist) {
getSessionRedis().set(uuid, toJson(thing), shouldExist ? "XX" : "NX", "EX", getSessionTimeout(thing));
getSessionRedis().set(uuid, toJson(thing), shouldExist ? XX : NX, EX, getSessionTimeout(thing));
} }


private long getSessionTimeout(T thing) { return getSessionTimeout(); } private long getSessionTimeout(T thing) { return getSessionTimeout(); }


+ 4
- 3
wizard-server/src/main/java/org/cobbzilla/wizard/dao/shard/cache/ShardCacheableFinder.java Dosyayı Görüntüle

@@ -12,6 +12,7 @@ import org.cobbzilla.wizard.model.shard.Shardable;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


import static org.cobbzilla.util.json.JsonUtil.toJsonOrDie; import static org.cobbzilla.util.json.JsonUtil.toJsonOrDie;
import static org.cobbzilla.wizard.cache.redis.RedisService.EX;
import static org.cobbzilla.wizard.dao.shard.AbstractShardedDAO.NULL_CACHE; import static org.cobbzilla.wizard.dao.shard.AbstractShardedDAO.NULL_CACHE;


@AllArgsConstructor @Accessors(chain=true) @AllArgsConstructor @Accessors(chain=true)
@@ -32,16 +33,16 @@ public abstract class ShardCacheableFinder<E extends Shardable, D extends Single
if (json == null) { if (json == null) {
entity = (E) find(args); entity = (E) find(args);
if (entity == null) { if (entity == null) {
shardedDAO.getShardCache().set(cacheKey, NULL_CACHE, "EX", getCacheTimeoutSeconds());
shardedDAO.getShardCache().set(cacheKey, NULL_CACHE, EX, getCacheTimeoutSeconds());
final String cacheRefsKey = shardedDAO.getCacheRefsKey(NULL_CACHE); final String cacheRefsKey = shardedDAO.getCacheRefsKey(NULL_CACHE);
shardedDAO.getShardCache().lpush(cacheRefsKey, cacheKey); shardedDAO.getShardCache().lpush(cacheRefsKey, cacheKey);
} else { } else {
shardedDAO.getShardCache().set(cacheKey, toJsonOrDie(entity), "EX", getCacheTimeoutSeconds());
shardedDAO.getShardCache().set(cacheKey, toJsonOrDie(entity), EX, getCacheTimeoutSeconds());
shardedDAO.getShardCache().lpush(shardedDAO.getCacheRefsKey(entity.getUuid()), cacheKey); shardedDAO.getShardCache().lpush(shardedDAO.getCacheRefsKey(entity.getUuid()), cacheKey);
} }
} else if (!json.equals(NULL_CACHE)) { } else if (!json.equals(NULL_CACHE)) {
entity = JsonUtil.fromJsonOrDie(json, shardedDAO.getEntityClass()); entity = JsonUtil.fromJsonOrDie(json, shardedDAO.getEntityClass());
shardedDAO.getShardCache().set(cacheKey, json, "EX", getCacheTimeoutSeconds());
shardedDAO.getShardCache().set(cacheKey, json, EX, getCacheTimeoutSeconds());
} }
return entity; return entity;
} }


Yükleniyor…
İptal
Kaydet