use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class RdbcacheApis method select_get.
/**
* select_get get multiple items
*
* To select one or more entries based on query string.
* It queries database and return immediately, and asynchronously saves the data to redis
*
* @param request HttpServletRequest
* @param opt1 String, can be expire or table or "sync" or "async"
* @param opt2 String, can be expire or table or "sync" or "async", but not otp1
* @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
* @return ResponseEntity
*/
@RequestMapping(value = { "/rdbcache/v1/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}", "/rdbcache/v1/select/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.GET)
public ResponseEntity<?> select_get(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3) {
if (request.getParameterMap().size() == 0) {
throw new BadRequestException("query string is needed, try add ?limit=256 to url");
}
Context context = new Context(true, true);
KvPairs pairs = new KvPairs();
AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
KeyInfo keyInfo = anyKey.getKeyInfo();
if (keyInfo.getQuery() == null && pairs.size() == 0) {
QueryInfo query = new QueryInfo(keyInfo.getTable());
query.setLimit(1024);
keyInfo.setQuery(query);
String msg = "no query string found, max rows limit is forced to 1024";
LOGGER.info(msg);
context.logTraceMessage(msg);
}
if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
LOGGER.debug("no record(s) found from database");
} else {
AppCtx.getAsyncOps().doSaveToRedis(context, pairs, anyKey);
}
return Response.send(context, pairs);
}
use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class RdbcacheApis method select_post.
/**
* select_post post multiple items
*
* To select one or more entries based on query string.
* It queries database and return immediately, and asynchronously saves the data to redis
*
* @param request HttpServletRequest
* @param opt1 String, can be expire or table or "sync" or "async"
* @param opt2 String, can be expire or table or "sync" or "async", but not otp1
* @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
* @param keys List, list of keys for returned entries
* @return ResponseEntity
*/
@RequestMapping(value = { "/rdbcache/v1/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}", "/rdbcache/v1/select/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> select_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody ArrayList<String> keys) {
if (request.getParameterMap().size() == 0) {
throw new BadRequestException("query string is needed");
}
Context context = new Context(true, true);
KvPairs pairs = new KvPairs(keys);
AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
KeyInfo keyInfo = anyKey.getKeyInfo();
if (keyInfo.getQuery() == null && pairs.size() == 0) {
QueryInfo query = new QueryInfo(keyInfo.getTable());
query.setLimit(1024);
keyInfo.setQuery(query);
String msg = "no query string found, max rows limit is forced to 1024";
LOGGER.info(msg);
context.logTraceMessage(msg);
}
if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
LOGGER.debug("no record(s) found from database");
} else {
AppCtx.getAsyncOps().doSaveToRedis(context, pairs, anyKey);
}
return Response.send(context, pairs);
}
use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class RdbcacheApis method pull_post.
/**
* pull_post post multiple items
*
* To pull one or more entries based on input keys. No * key. No query string.
* Once data found, it returns immediately. It queries redis first, then database.
*
* @param request HttpServletRequest
* @param opt1 String, can be expire or table or "sync" or "async"
* @param opt2 String, can be expire or table or "sync" or "async", but not otp1
* @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
* @return ResponseEntity
*/
@RequestMapping(value = { "/rdbcache/v1/pull", "/rdbcache/v1/pull/{opt1}", "/rdbcache/v1/pull/{opt1}/{opt2}", "/rdbcache/v1/pull/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> pull_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody ArrayList<String> keys) {
if (request.getParameterMap().size() != 0) {
throw new BadRequestException("query string is not supported");
}
if (keys == null || keys.size() == 0) {
throw new BadRequestException("missing keys");
}
if (keys.contains("*")) {
throw new BadRequestException("no * allowed as key");
}
if (request.getParameterMap().size() > 0) {
throw new BadRequestException("query string is not supported");
}
Context context = new Context(true, true);
KvPairs pairs = new KvPairs(keys);
AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
if (anyKey.size() != pairs.size()) {
throw new NotFoundException("one or more keys not found");
}
for (int i = 0; i < anyKey.size(); i++) {
KvPair pair = pairs.get(i);
KeyInfo keyInfo = anyKey.get(i);
if (keyInfo.getIsNew()) {
throw new NotFoundException("key not found for " + pair.getId());
}
}
LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
if (!AppCtx.getRedisRepo().find(context, pairs, anyKey)) {
KvPairs dbPairs = new KvPairs();
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
if (!pair.hasContent()) {
KeyInfo keyInfo = anyKey.get(i);
KvPairs pairsNew = new KvPairs(pair);
AnyKey anyKeyNew = new AnyKey(keyInfo);
if (AppCtx.getDbaseRepo().find(context, pairsNew, anyKeyNew)) {
dbPairs.add(pair);
}
}
}
if (dbPairs.size() > 0) {
AppCtx.getAsyncOps().doSaveToRedis(context, dbPairs, anyKey);
}
}
return Response.send(context, pairs);
}
use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class RedisRepoImpl method findAndSave.
@Override
public boolean findAndSave(final Context context, final KvPairs pairs, final AnyKey anyKey) {
if (LOGGER.isTraceEnabled()) {
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();
String type = pair.getType();
String hashKey = hdataPrefix + "::" + type + ":" + key;
KeyInfo keyInfo = anyKey.getAny(i);
Map<String, Object> map = pair.getData();
Map<String, Object> fmap = null;
if (enableDataCache) {
fmap = AppCtx.getCacheOps().getData(pair.getIdType());
if (fmap != null && fmap.size() > 0) {
LOGGER.debug("findAndSave - found from cache " + key);
}
AppCtx.getCacheOps().putData(pair, keyInfo);
}
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);
}
}
pair.setData(fmap);
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.getCacheOps().removeData(pair.getIdType());
}
allOk = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
e.printStackTrace();
}
if (allOk) {
allOk = fmap != null && fmap.size() > 0;
}
}
if (LOGGER.isTraceEnabled())
LOGGER.trace("findAndSave returns " + allOk);
return allOk;
}
use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class CacheOps method getKeyInfo.
public KeyInfo getKeyInfo(KvIdType idType) {
String hashKey = "keyInfo::" + idType.getType() + ":" + idType.getId();
Map<String, Object> map = get(hashKey);
if (map == null) {
return null;
}
KeyInfo keyInfo = Utils.toPojo(map, KeyInfo.class);
return keyInfo;
}
Aggregations