Search in sources :

Example 6 with StopWatch

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

the class ExpireOps method setExpireKey.

// set up expire key
// 
// expire = X,  it schedules an event in X seconds, only if not such event exists.
// The expiration event will happen at X seconds.
// 
// expire = +X, it schedules an event in X seconds always, even if such event exists.
// It may use to delay the event, if next call happens before X seconds.
// 
// expire = -X, it schedules a repeat event to occur every X seconds.
// 
// expire = 0,  it removes existing event and not to set any event
// 
public void setExpireKey(Context context, KvPairs pairs, AnyKey anyKey) {
    for (int i = 0; i < pairs.size(); i++) {
        try {
            KvPair pair = pairs.get(i);
            String key = pair.getId();
            KeyInfo keyInfo = anyKey.getAny(i);
            LOGGER.trace("setExpireKey: " + pair.printKey() + " expire: " + keyInfo.getExpire());
            String expire = keyInfo.getExpire();
            String expKey = eventPrefix + "::" + key;
            StopWatch stopWatch = context.startStopWatch("redis", "scriptExecutor.execute");
            Long result = scriptExecutor.execute(set_expire_key_script, Collections.singletonList(expKey), context.getTraceId(), expire);
            if (stopWatch != null)
                stopWatch.stopNow();
            if (result != 1) {
                keyInfo.restoreExpire();
            }
            if (keyInfo.getIsNew()) {
                AppCtx.getKeyInfoRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
            }
        } catch (Exception e) {
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
        }
    }
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) StopWatch(com.rdbcache.models.StopWatch)

Example 7 with StopWatch

use of com.rdbcache.models.StopWatch 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)

Example 8 with StopWatch

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

the class RedisRepoImpl method save.

@Override
public boolean save(Context context, KvPairs pairs, AnyKey anyKey) {
    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();
        KeyInfo keyInfo = anyKey.getAny(i);
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = pair.getData();
        if (enableDataCache) {
            AppCtx.getLocalCache().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.getLocalCache().removeData(key);
            }
            savedAll = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    }
    LOGGER.trace("save returns " + savedAll);
    return savedAll;
}
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)

Example 9 with StopWatch

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

the class RedisRepoImpl method ifExist.

@Override
public boolean ifExist(Context context, KvPairs pairs, AnyKey anyKey) {
    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();
        if (enableDataCache) {
            if (AppCtx.getLocalCache().containsData(key)) {
                LOGGER.trace("ifExist found from cache " + key);
                continue;
            }
        }
        StopWatch stopWatch = context.startStopWatch("redis", "stringRedisTemplate.hasKey");
        foundAll = AppCtx.getStringRedisTemplate().hasKey(hdataPrefix + "::" + key);
        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(com.rdbcache.models.KvPair) StopWatch(com.rdbcache.models.StopWatch)

Example 10 with StopWatch

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

the class AsyncOps method doDeleteFromRedisAndDbase.

public void doDeleteFromRedisAndDbase(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("doDeleteFromRedisAndDbase: " + pairs.size());
    if (context.isSync()) {
        AppCtx.getRedisRepo().delete(context, pairs, anyKey);
        AppCtx.getDbaseRepo().delete(context, pairs, anyKey);
        AppCtx.getKeyInfoRepo().delete(context, pairs);
        for (KvPair pair : pairs) {
            pair.setType("info");
            StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
            AppCtx.getKvPairRepo().delete(pair);
            if (stopWatch != null)
                stopWatch.stopNow();
        }
        Utils.getExcutorService().submit(() -> {
            context.closeMonitor();
        });
        return;
    }
    Utils.getExcutorService().submit(() -> {
        AppCtx.getRedisRepo().delete(context, pairs, anyKey);
        AppCtx.getDbaseRepo().delete(context, pairs, anyKey);
        AppCtx.getKeyInfoRepo().delete(context, pairs);
        for (KvPair pair : pairs) {
            pair.setType("info");
            StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
            AppCtx.getKvPairRepo().delete(pair);
            if (stopWatch != null)
                stopWatch.stopNow();
        }
        context.closeMonitor();
    });
}
Also used : KvPair(com.rdbcache.models.KvPair) StopWatch(com.rdbcache.models.StopWatch)

Aggregations

KvPair (com.rdbcache.models.KvPair)14 StopWatch (com.rdbcache.models.StopWatch)14 KeyInfo (com.rdbcache.models.KeyInfo)9 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)8 SQLException (java.sql.SQLException)4 AnyKey (com.rdbcache.helpers.AnyKey)1 KvPairs (com.rdbcache.helpers.KvPairs)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1