Search in sources :

Example 21 with KeyInfo

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

the class MockRedis method mockKeyInfoRedisTemplate.

public static RedisKeyInfoTemplate mockKeyInfoRedisTemplate() {
    RedisKeyInfoTemplate template = mock(RedisKeyInfoTemplate.class, Mockito.RETURNS_DEEP_STUBS);
    HashOperations keyInfoOps = mock(HashOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForHash()).thenReturn(keyInfoOps);
    // mock HashOperations get
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String subKey = (String) args[1];
        LOGGER.trace("RedisKeyInfoTemplate HashOperations get " + key + " " + subKey);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        Map<String, Object> subMap = (Map<String, Object>) map.get(subKey);
        return Utils.toPojo(subMap, KeyInfo.class);
    }).when(keyInfoOps).get(anyString(), anyString());
    // mock HashOperations put
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String subKey = (String) args[1];
        LOGGER.trace("RedisKeyInfoTemplate HashOperations put " + key + " " + subKey);
        KeyInfo keyInfo = (KeyInfo) args[2];
        Map<String, Object> subMap = Utils.toMap(keyInfo);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            map = new LinkedHashMap<>();
            data.put(key, map);
        }
        map.put(subKey, subMap);
        return null;
    }).when(keyInfoOps).put(anyString(), anyString(), any(KeyInfo.class));
    // mock HashOperations putAll
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        Map<String, Object> subMaps = (Map<String, Object>) args[1];
        LOGGER.trace("RedisKeyInfoTemplate HashOperations putAll " + key + " " + subMaps.keySet());
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            map = new LinkedHashMap<>();
            data.put(key, map);
        }
        for (Map.Entry<String, Object> entry : subMaps.entrySet()) {
            String subKey = entry.getKey();
            KeyInfo keyInfo = (KeyInfo) entry.getValue();
            map.put(subKey, Utils.toMap(keyInfo));
        }
        return null;
    }).when(keyInfoOps).putAll(anyString(), anyMap());
    // mock HashOperations multiGet
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        List<String> keys = (List<String>) args[1];
        LOGGER.trace("RedisKeyInfoTemplate HashOperations multiGet " + key);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        List<KeyInfo> resultList = new ArrayList<>();
        if (map == null) {
            return resultList;
        }
        for (String subKey : keys) {
            Map<String, Object> subMap = (Map<String, Object>) map.get(subKey);
            if (subMap != null) {
                KeyInfo keyInfo = Utils.toPojo(subMap, KeyInfo.class);
                resultList.add(keyInfo);
            } else {
                resultList.add(null);
            }
        }
        return resultList;
    }).when(keyInfoOps).multiGet(anyString(), anyList());
    // mock HashOperations delete single
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String subKey = (String) args[1];
        LOGGER.trace("RedisKeyInfoTemplate HashOperations delete " + key + " " + subKey);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        map.remove(subKey);
        return null;
    }).when(keyInfoOps).delete(anyString(), anyString());
    // mock HashOperations delete multiple
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        List<String> keys = (List<String>) args[1];
        LOGGER.trace("RedisKeyInfoTemplate HashOperations delete " + key + " " + keys);
        for (String subKey : keys) {
            map.remove(subKey);
        }
        return null;
    }).when(keyInfoOps).delete(anyString(), anyList());
    return template;
}
Also used : KeyInfo(doitincloud.rdbcache.models.KeyInfo) HashOperations(org.springframework.data.redis.core.HashOperations)

Example 22 with KeyInfo

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

the class Request method process.

public static AnyKey process(Context context, HttpServletRequest request, KvPairs pairs, Optional<String>... opts) {
    LOGGER.info("URI: " + request.getRequestURI());
    if (PropCfg.getEnableMonitor())
        context.enableMonitor(request);
    // {expire, table}
    String[] options = { null, null };
    for (int i = 0; i < opts.length; i++) {
        Optional<String> opt = opts[i];
        if (opt != null && opt.isPresent()) {
            assignOption(context, opt.get(), options);
        }
    }
    AnyKey anyKey = new AnyKey();
    if (pairs == null) {
        return anyKey;
    }
    KeyInfo keyInfo = anyKey.getAny();
    String table = options[1];
    if (pairs.size() > 0) {
        if (table != null) {
            // populate table info into all pairs
            for (KvPair pair : pairs) {
                pair.setType(table);
            }
        }
        // find key info for the first pair
        // 
        KvPair pair = pairs.get(0);
        if (!pair.isNewUuid()) {
            AppCtx.getKeyInfoRepo().find(context, pair, keyInfo);
        }
    }
    processOptions(context, request, keyInfo, options);
    if (pairs.size() == 0) {
        return anyKey;
    }
    // 
    if (keyInfo.getQuery() != null) {
        return anyKey;
    }
    // 
    if (pairs.size() > 1) {
        AppCtx.getKeyInfoRepo().find(context, pairs, anyKey);
    }
    // 
    for (int i = 0; i < pairs.size() && i < anyKey.size(); i++) {
        keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            keyInfo.setIsNew(false);
            KvPair pair = pairs.get(i);
            AppCtx.getCacheOps().putKeyInfo(pair.getIdType(), keyInfo);
            keyInfo.setIsNew(true);
        }
    }
    if (anyKey.size() != 1 && pairs.size() != anyKey.size()) {
        throw new ServerErrorException(context, "case not supported, anyKey size(" + anyKey.size() + ") != 1 && pairs size(" + pairs.size() + ") != anyKey size(" + anyKey.size() + ")");
    }
    return anyKey;
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPair(doitincloud.rdbcache.models.KvPair) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Example 23 with KeyInfo

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

the class SimpleExpireOps method setExpireKey.

public void setExpireKey(Context context, KvPairs pairs, AnyKey anyKey) {
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        KeyInfo keyInfo = anyKey.getAny(i);
        LOGGER.debug("setExpireKey: " + key + " expire: " + keyInfo.getExpire());
        String expire = keyInfo.getExpire();
        String expKey = eventPrefix + "::" + key;
        boolean hasKey = AppCtx.getRedisRepo().ifExist(context, new KvPairs(expKey), new AnyKey(keyInfo));
        Long expValue = Long.valueOf(expire);
        boolean done = false;
        // remove existing expire key
        if (hasKey) {
            if (expValue <= 0L || expire.startsWith("+")) {
                AppCtx.getRedisRepo().delete(context, new KvPairs(expKey), new AnyKey(keyInfo));
            } else {
                // for unsigned expire, event existed, no update
                done = true;
            }
        }
        // zero means no expiration
        if (!done && expValue == 0L) {
            done = true;
        }
        if (!done) {
            if (expValue < 0) {
                expValue = -expValue;
            }
            LOGGER.debug("setup expire: " + key + " expire: " + keyInfo.getExpire());
            AppCtx.getStringRedisTemplate().opsForValue().set(expKey, expire, expValue);
        } else {
            keyInfo.restoreExpire();
        }
        if (keyInfo.getIsNew()) {
            LOGGER.debug("save keyInfo: " + key + " expire: " + keyInfo.getExpire());
            AppCtx.getKeyInfoRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
        }
    }
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 24 with KeyInfo

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

the class SimpleKeyInfoRepo method find.

@Override
public boolean find(Context context, KvPair pair, KeyInfo keyInfo) {
    LOGGER.trace("find: " + pair.printKey() + " " + keyInfo.toString());
    boolean foundAll = true;
    String key = pair.getId();
    Map<String, Object> map = (Map<String, Object>) data.get(key);
    if (map == null) {
        foundAll = false;
        LOGGER.trace("find: Not Found " + key);
    } else {
        KeyInfo keyInfo2 = Utils.toPojo(map, KeyInfo.class);
        keyInfo.copy(keyInfo2);
        LOGGER.trace("find: Found " + key);
    }
    return foundAll;
}
Also used : KeyInfo(doitincloud.rdbcache.models.KeyInfo) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with KeyInfo

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

the class SimpleKeyInfoRepo method save.

@Override
public boolean save(Context context, KvPairs pairs, AnyKey anyKey) {
    Assert.isTrue(anyKey.size() == pairs.size(), anyKey.size() + " != " + pairs.size() + ", only supports that pairs and anyKey have the same size");
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String key = pair.getId();
        Map<String, Object> map = Utils.toMap(keyInfo);
        data.put(key, map);
        LOGGER.trace("save: " + key);
    }
    return true;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo)

Aggregations

KeyInfo (doitincloud.rdbcache.models.KeyInfo)50 KvPair (doitincloud.rdbcache.models.KvPair)29 AnyKey (doitincloud.rdbcache.supports.AnyKey)21 Context (doitincloud.rdbcache.supports.Context)17 KvPairs (doitincloud.rdbcache.supports.KvPairs)17 Test (org.junit.Test)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)9 StopWatch (doitincloud.rdbcache.models.StopWatch)8 BadRequestException (doitincloud.commons.exceptions.BadRequestException)7 SQLException (java.sql.SQLException)4 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 NotFoundException (doitincloud.commons.exceptions.NotFoundException)1 CacheOps (doitincloud.rdbcache.services.CacheOps)1 DbaseOps (doitincloud.rdbcache.services.DbaseOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1