use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class QueryTest method deleteTest.
@Test
public void deleteTest() {
try {
Context context = new Context();
KvPairs pairs = new KvPairs();
AnyKey anyKey = new AnyKey();
KeyInfo keyInfo = new KeyInfo();
keyInfo.setExpire("100");
keyInfo.setTable("user_table");
String json1 = "{\"table\":\"user_table\",\"conditions\":{\"id\":{\"=\":[\"3\"]}}}";
QueryInfo queryInfo1 = Utils.toPojo(Utils.toMap(json1), QueryInfo.class);
keyInfo.setQuery(queryInfo1);
anyKey.setKeyInfo(keyInfo);
Query query = new Query(context, jdbcTemplate, pairs, anyKey);
assertTrue(query.ifSelectOk());
assertTrue(query.executeSelect());
// System.out.println(Utils.toJsonMap(pairs));
assertEquals(1, pairs.size());
String key = pairs.getPair().getId();
KvPair pair = new KvPair(key);
pairs.setPair(pair);
KeyInfo keyInfo2 = new KeyInfo();
keyInfo2.setExpire("100");
keyInfo2.setTable("user_table");
keyInfo2.setClause("id = ?");
keyInfo2.setParams(Arrays.asList("3"));
anyKey.setKeyInfo(keyInfo2);
query = new Query(context, jdbcTemplate, pairs, anyKey);
assertTrue(query.ifDeleteOk());
assertTrue(query.executeDelete());
pairs.clear();
query = new Query(context, jdbcTemplate, pairs, anyKey);
assertTrue(query.ifSelectOk());
assertFalse(query.executeSelect());
assertEquals(0, pairs.size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getCause().getMessage());
}
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class SimpleKeyInfoRepo method save.
@Override
public boolean save(Context context, KvPairs pairs, AnyKey anyKey) {
Assert.isTrue(anyKey.size() == pairs.size(), anyKey.size() + " != " + pairs.size() + ", only supports that pairs and anyKey have the same size");
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
KeyInfo keyInfo = anyKey.getAny(i);
String key = pair.getId();
Map<String, Object> map = Utils.toMap(keyInfo);
data.put(key, map);
LOGGER.trace("save: " + key);
}
return true;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class SimpleExpireOps method setExpireKey.
public void setExpireKey(Context context, KvPairs pairs, AnyKey anyKey) {
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
String key = pair.getId();
KeyInfo keyInfo = anyKey.getAny(i);
LOGGER.debug("setExpireKey: " + key + " expire: " + keyInfo.getExpire());
String expire = keyInfo.getExpire();
String expKey = eventPrefix + "::" + key;
boolean hasKey = AppCtx.getRedisRepo().ifExist(context, new KvPairs(expKey), new AnyKey(keyInfo));
Long expValue = Long.valueOf(expire);
boolean done = false;
// remove existing expire key
if (hasKey) {
if (expValue <= 0L || expire.startsWith("+")) {
AppCtx.getRedisRepo().delete(context, new KvPairs(expKey), new AnyKey(keyInfo));
} else {
// for unsigned expire, event existed, no update
done = true;
}
}
// zero means no expiration
if (!done && expValue == 0L) {
done = true;
}
if (!done) {
if (expValue < 0) {
expValue = -expValue;
}
LOGGER.debug("setup expire: " + key + " expire: " + keyInfo.getExpire());
AppCtx.getStringRedisTemplate().opsForValue().set(expKey, expire, expValue);
} else {
keyInfo.restoreExpire();
}
if (keyInfo.getIsNew()) {
LOGGER.debug("save keyInfo: " + key + " expire: " + keyInfo.getExpire());
AppCtx.getKeyInfoRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
}
}
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class Request method process.
public static AnyKey process(Context context, HttpServletRequest request, KvPairs pairs, Optional<String> opt1, Optional<String> opt2) {
LOGGER.info("URI: " + request.getRequestURI());
if (PropCfg.getEnableMonitor())
context.enableMonitor(request);
AnyKey anyKey = new AnyKey();
if (pairs == null) {
return anyKey;
}
// find the keyinfo for the first key
if (pairs.size() > 0) {
AppCtx.getKeyInfoRepo().find(context, new KvPairs(pairs.getPair()), anyKey);
}
KeyInfo keyInfo = anyKey.getAny();
Map<String, String[]> params = request.getParameterMap();
if ((params != null && params.size() > 0) || opt1 != null || opt2 != null) {
processOptions(context, keyInfo, params, opt1, opt2);
}
if (pairs.size() == 0 || context.getAction().startsWith("select_")) {
return anyKey;
}
// find keyinfo for the second key and after
if (pairs.size() > 1) {
AppCtx.getKeyInfoRepo().find(context, pairs, anyKey);
}
for (int i = 0; i < pairs.size() && i < anyKey.size(); i++) {
keyInfo = anyKey.get(i);
if (keyInfo.getIsNew()) {
keyInfo.setIsNew(false);
String key = pairs.get(i).getId();
AppCtx.getLocalCache().putKeyInfo(key, keyInfo);
keyInfo.setIsNew(true);
}
}
if (anyKey.size() != 1 && pairs.size() != anyKey.size()) {
throw new ServerErrorException(context, "case not supported, anyKey size(" + anyKey.size() + ") != 1 && pairs size(" + pairs.size() + ") != anyKey size(" + anyKey.size() + ")");
}
return anyKey;
}
Aggregations