Search in sources :

Example 6 with KvPairs

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());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KeyInfo(com.rdbcache.models.KeyInfo) KvPair(com.rdbcache.models.KvPair) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Example 7 with KvPairs

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));
        }
    }
}
Also used : AnyKey(com.rdbcache.helpers.AnyKey) KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) KvPairs(com.rdbcache.helpers.KvPairs)

Aggregations

AnyKey (com.rdbcache.helpers.AnyKey)7 KvPairs (com.rdbcache.helpers.KvPairs)7 KeyInfo (com.rdbcache.models.KeyInfo)7 KvPair (com.rdbcache.models.KvPair)6 Context (com.rdbcache.helpers.Context)5 Test (org.junit.Test)4 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)1 StopWatch (com.rdbcache.models.StopWatch)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1