use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class LocalCache method get.
public Map<String, Object> get(String key) {
Cached cached = cache.get(key);
if (cached == null) {
return null;
}
if (cached.isTimeout()) {
if (!cached.isRefreshable()) {
cache.remove(key);
return null;
} else {
try {
Map<String, Object> map = cached.refreshable.call();
cached.setMap(map);
cached.renew();
return map;
} catch (Exception e) {
String msg = e.getCause().getMessage();
LOGGER.error(msg);
throw new ServerErrorException(msg);
}
}
} else {
return cached.getMap();
}
}
use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class LocalCache method update.
public Map<String, Object> update(String key, Map<String, Object> update) {
Cached cached = cache.get(key);
if (cached == null) {
return null;
}
if (cached.isTimeout()) {
if (!cached.isRefreshable()) {
cache.remove(key);
return null;
} else {
try {
Map<String, Object> map = cached.refreshable.call();
if (map == null) {
cache.remove(key);
return null;
}
for (Map.Entry<String, Object> entry : update.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
cached.setMap(map);
cached.renew();
return map;
} catch (Exception e) {
String msg = e.getCause().getMessage();
LOGGER.error(msg);
throw new ServerErrorException(msg);
}
}
} else {
Map<String, Object> map = cached.getMap();
if (map != null) {
for (Map.Entry<String, Object> entry : update.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
}
return map;
}
}
use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class DbaseRepoImpl method kvSave.
private boolean kvSave(Context context, KvPairs pairs, AnyKey anyKey) {
LOGGER.trace("kvSave: " + pairs.printKey() + anyKey.print());
if (anyKey.size() == 0) {
LOGGER.trace("kvSave(0) Ok - nothing to save");
return true;
}
if (anyKey.size() == 1) {
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
try {
AppCtx.getKvPairRepo().save(pairs.getPair());
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.trace("kvSave(1) Ok");
return true;
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
} else {
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
try {
for (KvPair pair : pairs) {
AppCtx.getKvPairRepo().save(pair);
}
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.trace("kvSave(" + pairs.size() + ") Ok");
return true;
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
}
LOGGER.trace("kvSave(" + pairs.size() + ") failed");
return false;
}
use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class DbaseRepoImpl method kvDelete.
private boolean kvDelete(Context context, KvPairs pairs, AnyKey anyKey) {
LOGGER.trace("kvDelete: " + pairs.printKey() + anyKey.print());
if (anyKey.size() == 0) {
LOGGER.trace("kvDelete(0) Ok - nothing to delete");
return true;
}
if (anyKey.size() == 1) {
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
try {
AppCtx.getKvPairRepo().delete(pairs.getPair());
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.trace("kvDelete(1) Ok");
return true;
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
} else {
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
try {
for (KvPair pair : pairs) {
AppCtx.getKvPairRepo().delete(pair);
}
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.trace("kvDelete(" + pairs.size() + ") Ok");
return true;
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
}
LOGGER.trace("kvDelete(" + pairs.size() + ") failed");
return false;
}
use of com.rdbcache.exceptions.ServerErrorException 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;
}
Aggregations