Search in sources :

Example 1 with StopWatch

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

the class AsyncOps method deleteKvPairsKeyInfo.

public void deleteKvPairsKeyInfo(Context context, KvPairs pairs, AnyKey anyKey) {
    KvPairs toDeletePairs = new KvPairs();
    for (KvPair pair : pairs) {
        String kvType = "keyInfo";
        String type = pair.getType();
        if (!type.equals("data")) {
            kvType += ":" + type;
        }
        toDeletePairs.add(new KvPair(pair.getId(), kvType));
    }
    if (toDeletePairs.size() > 0) {
        StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.deleteAll");
        AppCtx.getKvPairRepo().deleteAll(toDeletePairs);
        if (stopWatch != null)
            stopWatch.stopNow();
    }
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 2 with StopWatch

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

the class RedisRepoImpl method update.

@Override
public boolean update(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        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.getCacheOps().updateData(pair);
        }
        String key = pair.getId();
        String type = pair.getType();
        String hashKey = hdataPrefix + "::" + type + ":" + 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(doitincloud.rdbcache.models.KvPair) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 3 with StopWatch

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

the class RedisRepoImpl method update.

@Override
public boolean update(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("update: : " + pair.printKey() + " " + keyInfo.toString());
    }
    if (enableDataCache) {
        AppCtx.getCacheOps().updateData(pair);
    }
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + 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();
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        e.printStackTrace();
        throw new ServerErrorException(context, msg);
    }
    LOGGER.debug("update returns true");
    return true;
}
Also used : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 4 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 KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("findAndSave: " + pair.printKey() + " " + keyInfo.toString());
    }
    boolean allOk = true;
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + key;
    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);
        }
    }
    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);
        }
    }
    if (enableDataCache) {
        AppCtx.getCacheOps().putData(pair, keyInfo);
    }
    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 : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 5 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 KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        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();
        String type = pair.getType();
        KeyInfo keyInfo = anyKey.getAny(i);
        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);
                }
                continue;
            }
        }
        if (map == null || map.size() == 0) {
            foundAll = false;
            LOGGER.debug("find - not found " + key);
            continue;
        }
    }
    LOGGER.trace("find returns " + foundAll);
    return foundAll;
}
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)

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