Search in sources :

Example 1 with StopWatch

use of com.rdbcache.models.StopWatch in project rdbcache by rdbcache.

the class Query method executeUpdate.

public boolean executeUpdate() {
    params = new ArrayList<>();
    boolean allOk = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String table = keyInfo.getTable();
        if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
            waitForParamsUpdate(pair.getId(), keyInfo);
        }
        // 
        if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
            allOk = false;
            String msg = "executeUpdate failed when calling prepareStandardClauseParams for " + pair.getId();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
            continue;
        }
        Map<String, Object> map = pair.getData();
        params.clear();
        String updates = "";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            params.add(entry.getValue());
            if (updates.length() != 0)
                updates += ", ";
            updates += entry.getKey() + " = ?";
        }
        params.addAll(keyInfo.getParams());
        String clause = keyInfo.getClause();
        sql = "update " + table + " set " + updates + " where " + clause + " limit 1";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.update");
        try {
            if (jdbcTemplate.update(sql, params.toArray()) > 0) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                LOGGER.trace("update " + pair.getId() + " from " + table);
                continue;
            } else {
                if (stopWatch != null)
                    stopWatch.stopNow();
                allOk = false;
            }
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
        keyInfo.setQueryKey(null);
    }
    return allOk;
}
Also used : KvPair(com.rdbcache.models.KvPair) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 2 with StopWatch

use of com.rdbcache.models.StopWatch in project rdbcache by rdbcache.

the class RedisRepoImpl method find.

@Override
public boolean find(Context context, KvPairs pairs, AnyKey anyKey) {
    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();
        KeyInfo keyInfo = anyKey.getAny(i);
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = null;
        if (enableDataCache) {
            map = (Map<String, Object>) AppCtx.getLocalCache().getData(key);
            if (map != null && map.size() > 0) {
                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) {
                    if (enableDataCache) {
                        AppCtx.getLocalCache().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;
        }
        pair.setData(map);
    }
    LOGGER.trace("find returns " + foundAll);
    return foundAll;
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) StopWatch(com.rdbcache.models.StopWatch)

Example 3 with StopWatch

use of com.rdbcache.models.StopWatch in project rdbcache by rdbcache.

the class RedisRepoImpl method update.

@Override
public boolean update(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("update 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);
        if (enableDataCache) {
            AppCtx.getLocalCache().updateData(pair);
        }
        String key = pair.getId();
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = pair.getData();
        StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
        try {
            hashOps.putAll(hashKey, map);
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.debug("update redis for " + key);
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            foundAll = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            e.printStackTrace();
            throw new ServerErrorException(context, msg);
        }
    }
    LOGGER.debug("update returns " + foundAll);
    return foundAll;
}
Also used : KvPair(com.rdbcache.models.KvPair) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) StopWatch(com.rdbcache.models.StopWatch)

Example 4 with StopWatch

use of com.rdbcache.models.StopWatch in project rdbcache by rdbcache.

the class RedisRepoImpl method delete.

@Override
public void delete(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("findAndSave pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    if (enableDataCache) {
        AppCtx.getLocalCache().removeKeyAndData(pairs);
    }
    Set<String> hashKeys = new HashSet<>();
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        hashKeys.add(hdataPrefix + "::" + key);
        String expKey = eventPrefix + "::" + key;
        // get existing expire key
        StopWatch stopWatch = context.startStopWatch("redis", "stringRedisTemplate.hasKey");
        Set<String> expKeys = AppCtx.getStringRedisTemplate().keys(expKey + "::*");
        if (stopWatch != null)
            stopWatch.stopNow();
        if (expKeys != null && expKeys.size() > 0) {
            stopWatch = context.startStopWatch("redis", "stringRedisTemplate.delete");
            AppCtx.getStringRedisTemplate().delete(expKeys);
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.debug("delete " + key);
        }
    }
    StopWatch stopWatch = context.startStopWatch("redis", "stringRedisTemplate.delete");
    AppCtx.getStringRedisTemplate().delete(hashKeys);
    if (stopWatch != null)
        stopWatch.stopNow();
    LOGGER.trace("delete done");
}
Also used : KvPair(com.rdbcache.models.KvPair) StopWatch(com.rdbcache.models.StopWatch)

Example 5 with StopWatch

use of com.rdbcache.models.StopWatch in project rdbcache by rdbcache.

the class AsyncOps method doDeleteFromRedis.

public void doDeleteFromRedis(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("doDeleteFromRedis: " + pairs.size());
    if (context.isSync()) {
        AppCtx.getRedisRepo().delete(context, pairs, anyKey);
        AppCtx.getKeyInfoRepo().delete(context, pairs);
        for (KvPair pair : pairs) {
            pair.setType("info");
            StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
            AppCtx.getKvPairRepo().delete(pair);
            if (stopWatch != null)
                stopWatch.stopNow();
        }
        Utils.getExcutorService().submit(() -> {
            context.closeMonitor();
        });
        return;
    }
    Utils.getExcutorService().submit(() -> {
        AppCtx.getRedisRepo().delete(context, pairs, anyKey);
        AppCtx.getKeyInfoRepo().delete(context, pairs);
        for (KvPair pair : pairs) {
            pair.setType("info");
            StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
            AppCtx.getKvPairRepo().delete(pair);
            if (stopWatch != null)
                stopWatch.stopNow();
        }
        context.closeMonitor();
    });
}
Also used : KvPair(com.rdbcache.models.KvPair) StopWatch(com.rdbcache.models.StopWatch)

Aggregations

KvPair (com.rdbcache.models.KvPair)14 StopWatch (com.rdbcache.models.StopWatch)14 KeyInfo (com.rdbcache.models.KeyInfo)9 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)8 SQLException (java.sql.SQLException)4 AnyKey (com.rdbcache.helpers.AnyKey)1 KvPairs (com.rdbcache.helpers.KvPairs)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1