use of com.rdbcache.models.StopWatch in project rdbcache by rdbcache.
the class ExpireOps method setExpireKey.
// set up expire key
//
// expire = X, it schedules an event in X seconds, only if not such event exists.
// The expiration event will happen at X seconds.
//
// expire = +X, it schedules an event in X seconds always, even if such event exists.
// It may use to delay the event, if next call happens before X seconds.
//
// expire = -X, it schedules a repeat event to occur every X seconds.
//
// expire = 0, it removes existing event and not to set any event
//
public void setExpireKey(Context context, KvPairs pairs, AnyKey anyKey) {
for (int i = 0; i < pairs.size(); i++) {
try {
KvPair pair = pairs.get(i);
String key = pair.getId();
KeyInfo keyInfo = anyKey.getAny(i);
LOGGER.trace("setExpireKey: " + pair.printKey() + " expire: " + keyInfo.getExpire());
String expire = keyInfo.getExpire();
String expKey = eventPrefix + "::" + key;
StopWatch stopWatch = context.startStopWatch("redis", "scriptExecutor.execute");
Long result = scriptExecutor.execute(set_expire_key_script, Collections.singletonList(expKey), context.getTraceId(), expire);
if (stopWatch != null)
stopWatch.stopNow();
if (result != 1) {
keyInfo.restoreExpire();
}
if (keyInfo.getIsNew()) {
AppCtx.getKeyInfoRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
}
} catch (Exception e) {
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
}
}
}
use of com.rdbcache.models.StopWatch 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.StopWatch 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.StopWatch in project rdbcache by rdbcache.
the class RedisRepoImpl method ifExist.
@Override
public boolean ifExist(Context context, KvPairs pairs, AnyKey anyKey) {
LOGGER.trace("ifExist pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
boolean foundAll = true;
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
String key = pair.getId();
if (enableDataCache) {
if (AppCtx.getLocalCache().containsData(key)) {
LOGGER.trace("ifExist found from cache " + key);
continue;
}
}
StopWatch stopWatch = context.startStopWatch("redis", "stringRedisTemplate.hasKey");
foundAll = AppCtx.getStringRedisTemplate().hasKey(hdataPrefix + "::" + key);
if (stopWatch != null)
stopWatch.stopNow();
if (!foundAll) {
LOGGER.debug("ifExit not found from redis " + key);
break;
} else {
LOGGER.debug("ifExit found redis " + key);
}
}
LOGGER.debug("ifExist returns " + foundAll);
return foundAll;
}
use of com.rdbcache.models.StopWatch in project rdbcache by rdbcache.
the class AsyncOps method doDeleteFromRedisAndDbase.
public void doDeleteFromRedisAndDbase(Context context, KvPairs pairs, AnyKey anyKey) {
LOGGER.trace("doDeleteFromRedisAndDbase: " + pairs.size());
if (context.isSync()) {
AppCtx.getRedisRepo().delete(context, pairs, anyKey);
AppCtx.getDbaseRepo().delete(context, pairs, anyKey);
AppCtx.getKeyInfoRepo().delete(context, pairs);
for (KvPair pair : pairs) {
pair.setType("info");
StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
AppCtx.getKvPairRepo().delete(pair);
if (stopWatch != null)
stopWatch.stopNow();
}
Utils.getExcutorService().submit(() -> {
context.closeMonitor();
});
return;
}
Utils.getExcutorService().submit(() -> {
AppCtx.getRedisRepo().delete(context, pairs, anyKey);
AppCtx.getDbaseRepo().delete(context, pairs, anyKey);
AppCtx.getKeyInfoRepo().delete(context, pairs);
for (KvPair pair : pairs) {
pair.setType("info");
StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
AppCtx.getKvPairRepo().delete(pair);
if (stopWatch != null)
stopWatch.stopNow();
}
context.closeMonitor();
});
}
Aggregations