Search in sources :

Example 16 with StopWatch

use of doitincloud.rdbcache.models.StopWatch 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;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 17 with StopWatch

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

the class RedisRepoImpl method ifExist.

@Override
public boolean ifExist(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("ifExist: " + pair.printKey() + " " + keyInfo.toString());
    }
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + key;
    if (enableDataCache) {
        if (AppCtx.getCacheOps().containsData(pair.getIdType())) {
            if (LOGGER.isTraceEnabled())
                LOGGER.trace("ifExist found from cache " + key);
            return true;
        }
    }
    StopWatch stopWatch = context.startStopWatch("redis", "stringRedisTemplate.hasKey");
    boolean hasIt = AppCtx.getStringRedisTemplate().hasKey(hashKey);
    if (stopWatch != null)
        stopWatch.stopNow();
    if (!hasIt) {
        LOGGER.debug("ifExit not found from redis " + key);
        return false;
    } else {
        LOGGER.debug("ifExit found redis " + key);
        return true;
    }
}
Also used : StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 18 with StopWatch

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

the class RedisRepoImpl method save.

@Override
public boolean save(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("save: " + pair.printKey() + " " + keyInfo.toString());
    }
    boolean savedAll = true;
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + key;
    Map<String, Object> map = pair.getData();
    if (enableDataCache) {
        AppCtx.getCacheOps().putData(pair, keyInfo);
    }
    StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
    try {
        hashOps.putAll(hashKey, map);
        if (stopWatch != null)
            stopWatch.stopNow();
        LOGGER.debug("save to redis for " + key);
    } catch (Exception e) {
        if (stopWatch != null)
            stopWatch.stopNow();
        if (enableDataCache) {
            AppCtx.getCacheOps().removeData(pair.getIdType());
        }
        savedAll = false;
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        context.logTraceMessage(msg);
        e.printStackTrace();
        if (context.isSync()) {
            throw new ServerErrorException(context, msg);
        }
    }
    if (LOGGER.isTraceEnabled())
        LOGGER.trace("save returns " + savedAll);
    return savedAll;
}
Also used : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 19 with StopWatch

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

the class ExpireOps method setExpireKey.

// set up expire key
// 
// expire = X,  it schedules an event in X seconds, only if not such event exists.
// The expiration event will happen at X seconds.
// 
// expire = +X, it schedules an event in X seconds always, even if such event exists.
// It may use to delay the event, if next call happens before X seconds.
// 
// expire = -X, it schedules a repeat event to occur every X seconds.
// 
// expire = 0,  it removes existing event and not to set any event
// 
public void setExpireKey(Context context, KvPair pair, KeyInfo keyInfo) {
    try {
        String key = pair.getId();
        String type = pair.getType();
        LOGGER.trace("setExpireKey: " + pair.printKey() + " expire: " + keyInfo.getExpire());
        String expire = keyInfo.getExpire();
        String expKey = eventPrefix + "::" + type + ":" + key;
        StopWatch stopWatch = context.startStopWatch("redis", "scriptExecutor.execute");
        Long result = scriptExecutor.execute(set_expire_key_script, Collections.singletonList(expKey), context.getTraceId(), expire);
        if (stopWatch != null)
            stopWatch.stopNow();
        if (result != 1) {
            keyInfo.restoreExpire();
        }
        if (keyInfo.getIsNew()) {
            AppCtx.getKeyInfoRepo().save(context, pair, keyInfo);
        }
    } catch (Exception e) {
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        context.logTraceMessage(msg);
    }
}
Also used : StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 20 with StopWatch

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

the class Query method executeSelect.

public boolean executeSelect() {
    KeyInfo keyInfo = anyKey.getKeyInfo();
    String table = keyInfo.getTable();
    LOGGER.trace("sql: " + sql);
    LOGGER.trace("params: " + (params != null ? params.toString() : "null"));
    List<Map<String, Object>> list = null;
    StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.queryForList");
    try {
        if (params != null) {
            list = jdbcTemplate.queryForList(sql, params.toArray());
        } else {
            list = jdbcTemplate.queryForList(sql);
        }
        if (stopWatch != null)
            stopWatch.stopNow();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                KvPair pair = pairs.getAny(i);
                if (pair.getType().equals("data")) {
                    pair.setType(table);
                }
                pair.setData(list.get(i));
                keyInfo = anyKey.getAny(i);
                // 
                if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
                    String msg = "executeSelect failed when prepareStandardClauseParams for " + pair.getId();
                    LOGGER.error(msg);
                    context.logTraceMessage(msg);
                    if (context.isSync()) {
                        throw new ServerErrorException(context, msg);
                    }
                }
                LOGGER.trace("found " + pair.getId() + " from " + table);
            }
            return true;
        }
    } catch (Exception e) {
        if (stopWatch != null)
            stopWatch.stopNow();
        e.printStackTrace();
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        context.logTraceMessage(msg);
        if (context.isSync()) {
            throw new ServerErrorException(context, msg);
        }
    }
    return false;
}
Also used : KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPair(doitincloud.rdbcache.models.KvPair) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) SQLException(java.sql.SQLException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Aggregations

StopWatch (doitincloud.rdbcache.models.StopWatch)22 KvPair (doitincloud.rdbcache.models.KvPair)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)12 KeyInfo (doitincloud.rdbcache.models.KeyInfo)8 SQLException (java.sql.SQLException)5 KvPairs (doitincloud.rdbcache.supports.KvPairs)3 AnyKey (doitincloud.rdbcache.supports.AnyKey)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)2 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)2 KeyHolder (org.springframework.jdbc.support.KeyHolder)2 Monitor (doitincloud.rdbcache.models.Monitor)1 Context (doitincloud.rdbcache.supports.Context)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ApplicationContext (org.springframework.context.ApplicationContext)1