Search in sources :

Example 41 with KvPair

use of doitincloud.rdbcache.models.KvPair 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 42 with KvPair

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

the class ExpireOps method setExpireKey.

public void setExpireKey(Context context, KvPairs pairs, AnyKey anyKey) {
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        setExpireKey(context, pair, keyInfo);
    }
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo)

Example 43 with KvPair

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

the class AbsDbaseRepo method update.

@Override
public boolean update(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    boolean allOk = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        if (!update(context, pair, keyInfo)) {
            allOk = false;
        }
    }
    return allOk;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo)

Example 44 with KvPair

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

the class AbsDbaseRepo method delete.

@Override
public boolean delete(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    boolean allOk = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        if (!delete(context, pair, keyInfo)) {
            allOk = false;
        }
    }
    return allOk;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo)

Example 45 with KvPair

use of doitincloud.rdbcache.models.KvPair 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

KvPair (doitincloud.rdbcache.models.KvPair)51 KeyInfo (doitincloud.rdbcache.models.KeyInfo)29 KvPairs (doitincloud.rdbcache.supports.KvPairs)22 Context (doitincloud.rdbcache.supports.Context)15 Test (org.junit.Test)15 AnyKey (doitincloud.rdbcache.supports.AnyKey)14 StopWatch (doitincloud.rdbcache.models.StopWatch)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)9 BadRequestException (doitincloud.commons.exceptions.BadRequestException)7 KvIdType (doitincloud.rdbcache.models.KvIdType)6 SQLException (java.sql.SQLException)4 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 NotFoundException (doitincloud.commons.exceptions.NotFoundException)1