Search in sources :

Example 6 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class LocalCache method get.

public Map<String, Object> get(String key) {
    Cached cached = cache.get(key);
    if (cached == null) {
        return null;
    }
    if (cached.isTimeout()) {
        if (!cached.isRefreshable()) {
            cache.remove(key);
            return null;
        } else {
            try {
                Map<String, Object> map = cached.refreshable.call();
                cached.setMap(map);
                cached.renew();
                return map;
            } catch (Exception e) {
                String msg = e.getCause().getMessage();
                LOGGER.error(msg);
                throw new ServerErrorException(msg);
            }
        }
    } else {
        return cached.getMap();
    }
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 7 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class LocalCache method update.

public Map<String, Object> update(String key, Map<String, Object> update) {
    Cached cached = cache.get(key);
    if (cached == null) {
        return null;
    }
    if (cached.isTimeout()) {
        if (!cached.isRefreshable()) {
            cache.remove(key);
            return null;
        } else {
            try {
                Map<String, Object> map = cached.refreshable.call();
                if (map == null) {
                    cache.remove(key);
                    return null;
                }
                for (Map.Entry<String, Object> entry : update.entrySet()) {
                    map.put(entry.getKey(), entry.getValue());
                }
                cached.setMap(map);
                cached.renew();
                return map;
            } catch (Exception e) {
                String msg = e.getCause().getMessage();
                LOGGER.error(msg);
                throw new ServerErrorException(msg);
            }
        }
    } else {
        Map<String, Object> map = cached.getMap();
        if (map != null) {
            for (Map.Entry<String, Object> entry : update.entrySet()) {
                map.put(entry.getKey(), entry.getValue());
            }
        }
        return map;
    }
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 8 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class DbaseRepoImpl method kvSave.

private boolean kvSave(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("kvSave: " + pairs.printKey() + anyKey.print());
    if (anyKey.size() == 0) {
        LOGGER.trace("kvSave(0) Ok - nothing to save");
        return true;
    }
    if (anyKey.size() == 1) {
        StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
        try {
            AppCtx.getKvPairRepo().save(pairs.getPair());
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.trace("kvSave(1) Ok");
            return true;
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    } else {
        StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
        try {
            for (KvPair pair : pairs) {
                AppCtx.getKvPairRepo().save(pair);
            }
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.trace("kvSave(" + pairs.size() + ") Ok");
            return true;
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    }
    LOGGER.trace("kvSave(" + pairs.size() + ") failed");
    return false;
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 9 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class DbaseRepoImpl method kvDelete.

private boolean kvDelete(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("kvDelete: " + pairs.printKey() + anyKey.print());
    if (anyKey.size() == 0) {
        LOGGER.trace("kvDelete(0) Ok - nothing to delete");
        return true;
    }
    if (anyKey.size() == 1) {
        StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
        try {
            AppCtx.getKvPairRepo().delete(pairs.getPair());
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.trace("kvDelete(1) Ok");
            return true;
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    } else {
        StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
        try {
            for (KvPair pair : pairs) {
                AppCtx.getKvPairRepo().delete(pair);
            }
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.trace("kvDelete(" + pairs.size() + ") Ok");
            return true;
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    }
    LOGGER.trace("kvDelete(" + pairs.size() + ") failed");
    return false;
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 10 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class RedisRepoImpl method findAndSave.

@Override
public boolean findAndSave(Context context, KvPairs pairs, AnyKey anyKey) {
    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();
        KeyInfo keyInfo = anyKey.getAny(i);
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = pair.getData();
        Map<String, Object> fmap = null;
        if (enableDataCache) {
            fmap = AppCtx.getLocalCache().getData(key);
            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.getLocalCache().putData(pair, keyInfo);
        }
        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.getLocalCache().removeData(key);
            }
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            e.printStackTrace();
        }
        if (fmap != null && fmap.size() > 0) {
            if (allOk)
                pair.setData(fmap);
        } else {
            allOk = false;
        }
    }
    LOGGER.trace("findAndSave returns " + allOk);
    return allOk;
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) StopWatch(com.rdbcache.models.StopWatch)

Aggregations

ServerErrorException (com.rdbcache.exceptions.ServerErrorException)18 KeyInfo (com.rdbcache.models.KeyInfo)9 KvPair (com.rdbcache.models.KvPair)8 StopWatch (com.rdbcache.models.StopWatch)8 SQLException (java.sql.SQLException)4 AnyKey (com.rdbcache.helpers.AnyKey)1 KvPairs (com.rdbcache.helpers.KvPairs)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 EventListener (org.springframework.context.event.EventListener)1 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1