Search in sources :

Example 11 with KvPair

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

the class KvPairRepoImpl method save.

@Override
public boolean save(KvPair pair) {
    KvPair dbPair = findById(pair.getIdType());
    if (dbPair == null) {
        try {
            String sql = "insert into " + table + " (id, type, value) values (?, ?, ?)";
            Object[] params = new Object[] { pair.getId(), pair.getType(), pair.getValue() };
            int result = jdbcTemplate.update(sql, params);
            return result == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    } else if (!DbUtils.isMapEquals(pair.getData(), dbPair.getData())) {
        try {
            String sql = "update " + table + " set value = ? where id = ? AND type = ?";
            Object[] params = new Object[] { pair.getValue(), pair.getId(), pair.getType() };
            int result = jdbcTemplate.update(sql, params);
            return result == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 12 with KvPair

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

the class KvPairRepoImpl method findById.

@Override
public KvPair findById(KvIdType idType) {
    try {
        String sql = "select value from " + table + " where id = ? AND type = ?";
        Object[] params = new Object[] { idType.getId(), idType.getType() };
        String value = jdbcTemplate.queryForObject(sql, params, String.class);
        if (value == null) {
            return null;
        }
        KvPair pair = new KvPair(idType);
        pair.setValue(value);
        return pair;
    } catch (EmptyResultDataAccessException e) {
        return null;
    }
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 13 with KvPair

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

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

Example 15 with KvPair

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

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