Search in sources :

Example 6 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 KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("save pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    }
    boolean savedAll = 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();
        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 : 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 7 with StopWatch

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

the class RedisRepoImpl method find.

@Override
public boolean find(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("find: " + pair.printKey() + " " + keyInfo.toString());
    }
    boolean foundAll = true;
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + key;
    Map<String, Object> map = null;
    if (enableDataCache) {
        map = (Map<String, Object>) AppCtx.getCacheOps().getData(pair.getIdType());
        if (map != null && map.size() > 0) {
            pair.setData(map);
            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) {
                pair.setData(map);
                if (enableDataCache) {
                    AppCtx.getCacheOps().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);
            }
        }
    }
    if (map == null || map.size() == 0) {
        foundAll = false;
        LOGGER.debug("find - not found " + key);
    }
    if (LOGGER.isTraceEnabled())
        LOGGER.trace("find returns " + foundAll);
    return foundAll;
}
Also used : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 8 with StopWatch

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

the class RedisRepoImpl method delete.

@Override
public void delete(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());
    }
    if (enableDataCache) {
        AppCtx.getCacheOps().removeKeyAndData(pairs);
    }
    Set<String> hashKeys = new HashSet<>();
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        String type = pair.getType();
        hashKeys.add(hdataPrefix + "::" + type + ":" + key);
        String expKey = eventPrefix + "::" + type + ":" + 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(doitincloud.rdbcache.models.KvPair) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 9 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 KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("ifExist 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();
        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);
                continue;
            }
        }
        StopWatch stopWatch = context.startStopWatch("redis", "stringRedisTemplate.hasKey");
        foundAll = AppCtx.getStringRedisTemplate().hasKey(hashKey);
        if (stopWatch != null)
            stopWatch.stopNow();
        if (!foundAll) {
            LOGGER.debug("ifExit not found from redis " + key);
            break;
        } else {
            LOGGER.debug("ifExit found redis " + key);
        }
    }
    LOGGER.debug("ifExist returns " + foundAll);
    return foundAll;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 10 with StopWatch

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

the class RedisRepoImpl method delete.

@Override
public void delete(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("findAndSave: " + pair.printKey() + " " + keyInfo.toString());
    }
    if (enableDataCache) {
        AppCtx.getCacheOps().removeKeyAndData(pair);
    }
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + key;
    String expKey = eventPrefix + "::" + type + ":" + 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 = context.startStopWatch("redis", "stringRedisTemplate.delete");
    AppCtx.getStringRedisTemplate().delete(hashKey);
    if (stopWatch != null)
        stopWatch.stopNow();
    LOGGER.trace("delete done");
}
Also used : 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