use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class RedisRepoImpl method findAndSave.
@Override
public boolean findAndSave(Context context, KvPairs pairs, AnyKey anyKey) {
LOGGER.trace("findAndSave pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
boolean allOk = true;
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
String key = pair.getId();
KeyInfo keyInfo = anyKey.getAny(i);
String hashKey = hdataPrefix + "::" + key;
Map<String, Object> map = pair.getData();
Map<String, Object> fmap = null;
if (enableDataCache) {
fmap = AppCtx.getLocalCache().getData(key);
if (fmap != null && fmap.size() > 0) {
LOGGER.debug("findAndSave - found from cache " + key);
}
}
StopWatch stopWatch = null;
if (fmap == null) {
try {
stopWatch = context.startStopWatch("redis", "hashOps.entries");
fmap = hashOps.entries(hashKey);
if (stopWatch != null)
stopWatch.stopNow();
if (fmap != null && fmap.size() > 0) {
LOGGER.debug("findAndSave - found from redis " + key);
}
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
e.printStackTrace();
throw new ServerErrorException(context, msg);
}
}
if (enableDataCache) {
AppCtx.getLocalCache().putData(pair, keyInfo);
}
try {
stopWatch = context.startStopWatch("redis", "hashOps.putAll");
hashOps.putAll(hashKey, map);
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.debug("findAndSave - save " + key);
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
if (enableDataCache) {
AppCtx.getLocalCache().removeData(key);
}
allOk = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
e.printStackTrace();
}
if (fmap != null && fmap.size() > 0) {
if (allOk)
pair.setData(fmap);
} else {
allOk = false;
}
}
LOGGER.trace("findAndSave returns " + allOk);
return allOk;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class RedisRepoImpl method save.
@Override
public boolean save(Context context, KvPairs pairs, AnyKey anyKey) {
LOGGER.trace("save pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
boolean savedAll = true;
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
String key = pair.getId();
KeyInfo keyInfo = anyKey.getAny(i);
String hashKey = hdataPrefix + "::" + key;
Map<String, Object> map = pair.getData();
if (enableDataCache) {
AppCtx.getLocalCache().putData(pair, keyInfo);
}
StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
try {
hashOps.putAll(hashKey, map);
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.debug("save to redis for " + key);
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
if (enableDataCache) {
AppCtx.getLocalCache().removeData(key);
}
savedAll = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
}
LOGGER.trace("save returns " + savedAll);
return savedAll;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class ExpireOps method onExpireEvent.
/**
* To process key expired event
*
* @param event key expired event
*/
public void onExpireEvent(String event) {
LOGGER.debug("Received: " + event);
if (!event.startsWith(eventPrefix)) {
return;
}
String[] parts = event.split("::");
if (parts.length < 3) {
LOGGER.error("invalid event format");
return;
}
String key = parts[1];
String traceId = parts[2];
Context context = new Context(traceId);
KvPair pair = new KvPair(key);
if (enableMonitor)
context.enableMonitor(event, "event", key);
String lockKey = "lock_" + eventPrefix + "::" + key + "::" + traceId;
String signature = Utils.generateId();
StopWatch stopWatch = context.startStopWatch("redis", "scriptExecutor.execute");
String result = scriptExecutor.execute(expire_event_lock_script, Collections.singletonList(lockKey), signature, eventLockTimeout.toString());
if (stopWatch != null)
stopWatch.stopNow();
if (!result.equals("OK")) {
String msg = "unable to lock key: " + lockKey;
LOGGER.trace(msg);
context.closeMonitor();
return;
}
try {
KvPairs pairs = new KvPairs(pair);
AnyKey anyKey = new AnyKey();
if (!AppCtx.getKeyInfoRepo().find(context, pairs, anyKey)) {
String msg = "keyInfo not found";
LOGGER.error(msg);
context.logTraceMessage(msg);
return;
}
KeyInfo keyInfo = anyKey.getKeyInfo();
LOGGER.trace(keyInfo.toString());
Long expire = Long.valueOf(keyInfo.getExpire());
if (expire > 0) {
if (AppCtx.getRedisRepo().find(context, pairs, anyKey)) {
AppCtx.getDbaseRepo().save(context, pairs, anyKey);
AppCtx.getRedisRepo().delete(context, pairs, anyKey);
AppCtx.getKeyInfoRepo().delete(context, pairs);
} else {
String msg = "failed to find key from redis for " + key;
LOGGER.error(msg);
context.logTraceMessage(msg);
}
}
if (expire < 0) {
if (AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
AppCtx.getRedisRepo().save(context, pairs, anyKey);
setExpireKey(context, pairs, anyKey);
} else {
String msg = "failed to find key from database for " + key;
LOGGER.error(msg);
context.logTraceMessage(msg);
}
}
} catch (Exception e) {
e.printStackTrace();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
} finally {
stopWatch = context.startStopWatch("redis", "scriptExecutor.execute");
scriptExecutor.execute(expire_event_unlock_script, Collections.singletonList(lockKey), signature);
if (stopWatch != null)
stopWatch.stopNow();
context.closeMonitor();
}
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class LocalCache method getKeyInfo.
public KeyInfo getKeyInfo(String key) {
Map<String, Object> map = get("key::" + key);
if (map == null) {
return null;
}
KeyInfo keyInfo = Utils.toPojo(map, KeyInfo.class);
return keyInfo;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class TaskQueue method onReceiveTask.
public void onReceiveTask(String task) {
LOGGER.debug("Received Task: " + task);
String[] parts = task.split("::");
if (parts.length < 3) {
LOGGER.error("invalid task format");
return;
}
String action = parts[0];
String key = parts[1];
String traceId = parts[2];
Context context = new Context(traceId);
if (enableMonitor)
context.enableMonitor(task, "queue", action);
KvPair pair = new KvPair(key);
KvPairs pairs = new KvPairs(pair);
AnyKey anyKey = new AnyKey();
if (!AppCtx.getKeyInfoRepo().find(context, pairs, anyKey)) {
String msg = "keyInfo not found";
LOGGER.error(msg);
context.logTraceMessage(msg);
return;
}
KeyInfo keyInfo = anyKey.getKeyInfo();
// ...
String msg = "unknown task action:" + action;
LOGGER.error(msg);
context.logTraceMessage(msg);
context.closeMonitor();
}
Aggregations