use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class RedisRepoImpl method find.
@Override
public boolean find(final Context context, final KvPairs pairs, final AnyKey anyKey) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("find 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();
String type = pair.getType();
KeyInfo keyInfo = anyKey.getAny(i);
String hashKey = hdataPrefix + "::" + type + ":" + key;
Map<String, Object> map = null;
if (enableDataCache) {
map = (Map<String, Object>) AppCtx.getCacheOps().getData(pair.getIdType());
if (map != null && map.size() > 0) {
pair.setData(map);
LOGGER.debug("find - found from cache " + key);
}
}
if (map == null) {
StopWatch stopWatch = context.startStopWatch("redis", "hashOps.entries");
try {
map = hashOps.entries(hashKey);
if (stopWatch != null)
stopWatch.stopNow();
if (map != null && map.size() > 0) {
pair.setData(map);
if (enableDataCache) {
AppCtx.getCacheOps().putData(pair, keyInfo);
}
LOGGER.debug("find - found from redis " + key);
}
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
foundAll = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
continue;
}
}
if (map == null || map.size() == 0) {
foundAll = false;
LOGGER.debug("find - not found " + key);
continue;
}
}
LOGGER.trace("find returns " + foundAll);
return foundAll;
}
use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class RedisRepoImpl method save.
@Override
public boolean save(final Context context, final KvPairs pairs, final AnyKey anyKey) {
if (LOGGER.isTraceEnabled()) {
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();
String type = pair.getType();
String hashKey = hdataPrefix + "::" + type + ":" + key;
KeyInfo keyInfo = anyKey.getAny(i);
Map<String, Object> map = pair.getData();
if (enableDataCache) {
AppCtx.getCacheOps().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.getCacheOps().removeData(pair.getIdType());
}
savedAll = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
}
if (LOGGER.isTraceEnabled())
LOGGER.trace("save returns " + savedAll);
return savedAll;
}
use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class RedisRepoImpl method find.
@Override
public boolean find(final Context context, final KvPair pair, final KeyInfo keyInfo) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("find: " + pair.printKey() + " " + keyInfo.toString());
}
boolean foundAll = true;
String key = pair.getId();
String type = pair.getType();
String hashKey = hdataPrefix + "::" + type + ":" + key;
Map<String, Object> map = null;
if (enableDataCache) {
map = (Map<String, Object>) AppCtx.getCacheOps().getData(pair.getIdType());
if (map != null && map.size() > 0) {
pair.setData(map);
LOGGER.debug("find - found from cache " + key);
}
}
if (map == null) {
StopWatch stopWatch = context.startStopWatch("redis", "hashOps.entries");
try {
map = hashOps.entries(hashKey);
if (stopWatch != null)
stopWatch.stopNow();
if (map != null && map.size() > 0) {
pair.setData(map);
if (enableDataCache) {
AppCtx.getCacheOps().putData(pair, keyInfo);
}
LOGGER.debug("find - found from redis " + key);
}
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
foundAll = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
}
if (map == null || map.size() == 0) {
foundAll = false;
LOGGER.debug("find - not found " + key);
}
if (LOGGER.isTraceEnabled())
LOGGER.trace("find returns " + foundAll);
return foundAll;
}
use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class CacheOps 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 doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class CacheOps method put.
public Map<String, Object> put(String key, Long timeToLive, Callable<Map<String, Object>> refreshable) {
try {
Map<String, Object> map = refreshable.call();
if (map == null) {
return null;
}
Cached cached = new Cached(map, timeToLive);
cached.refreshable = refreshable;
cache.put(key, cached);
return map;
} catch (Exception e) {
String msg = e.getCause().getMessage();
LOGGER.error(msg);
throw new ServerErrorException(msg);
}
}
Aggregations