use of com.rdbcache.helpers.KvPairs 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.helpers.KvPairs 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));
}
}
}
Aggregations