Search in sources :

Example 6 with KeyInfo

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

the class Query method ifUpdateOk.

public boolean ifUpdateOk() {
    Assert.isTrue(anyKey.size() == pairs.size(), anyKey.size() + " != " + pairs.size() + ", update only supports anyKey size == pairs size");
    KeyInfo keyInfo = anyKey.getKeyInfo();
    String table = keyInfo.getTable();
    if (table == null) {
        return false;
    }
    String queryKey = keyInfo.getQueryKey();
    if (queryKey == null) {
        return false;
    }
    return true;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo)

Example 7 with KeyInfo

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

the class Query method ifInsertOk.

public boolean ifInsertOk() {
    Assert.isTrue(anyKey.size() == 1, "anyKey.size() = " + anyKey.size() + ", insert only supports anyKey size == 1");
    KeyInfo keyInfo = anyKey.getKeyInfo();
    String table = keyInfo.getTable();
    if (table == null) {
        return false;
    }
    String queryKey = keyInfo.getQueryKey();
    if (queryKey == null) {
        return false;
    }
    return true;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo)

Example 8 with KeyInfo

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

the class Query method getLimit.

private int getLimit() {
    KeyInfo keyInfo = anyKey.getKeyInfo();
    Integer queryLimit = keyInfo.getQueryLimit();
    int size = pairs.size();
    int limit = 0;
    if (queryLimit != null || size > 0) {
        if (size == 0) {
            limit = queryLimit;
        } else if (queryLimit != null && size > queryLimit) {
            limit = queryLimit;
        } else {
            limit = size;
        }
    }
    if (limit == 0 && !context.isBatch()) {
        limit = 1;
    }
    return limit;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo)

Example 9 with KeyInfo

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

the class RedisRepoImpl method find.

@Override
public boolean find(Context context, KvPairs pairs, AnyKey anyKey) {
    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();
        KeyInfo keyInfo = anyKey.getAny(i);
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = null;
        if (enableDataCache) {
            map = (Map<String, Object>) AppCtx.getLocalCache().getData(key);
            if (map != null && map.size() > 0) {
                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) {
                    if (enableDataCache) {
                        AppCtx.getLocalCache().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;
        }
        pair.setData(map);
    }
    LOGGER.trace("find returns " + foundAll);
    return foundAll;
}
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 10 with KeyInfo

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

Aggregations

KeyInfo (com.rdbcache.models.KeyInfo)44 KvPair (com.rdbcache.models.KvPair)23 Test (org.junit.Test)13 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)9 StopWatch (com.rdbcache.models.StopWatch)9 AnyKey (com.rdbcache.helpers.AnyKey)7 Context (com.rdbcache.helpers.Context)7 KvPairs (com.rdbcache.helpers.KvPairs)7 BadRequestException (com.rdbcache.exceptions.BadRequestException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 SQLException (java.sql.SQLException)4 QueryInfo (com.rdbcache.queries.QueryInfo)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 KeyInfoRedisTemplate (com.rdbcache.configs.KeyInfoRedisTemplate)1 NotFoundException (com.rdbcache.exceptions.NotFoundException)1 DbaseOps (com.rdbcache.services.DbaseOps)1 LocalCache (com.rdbcache.services.LocalCache)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1