Search in sources :

Example 1 with KeyInfoRedisTemplate

use of com.rdbcache.configs.KeyInfoRedisTemplate in project rdbcache by rdbcache.

the class KeyInfoRepoImpl method handleApplicationReadyEvent.

@EventListener
public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
    KeyInfoRedisTemplate template = AppCtx.getKeyInfoRedisTemplate();
    if (template == null) {
        LOGGER.error("failed to get key info redis template");
        return;
    }
    keyInfoOps = template.opsForHash();
}
Also used : KeyInfoRedisTemplate(com.rdbcache.configs.KeyInfoRedisTemplate) EventListener(org.springframework.context.event.EventListener)

Example 2 with KeyInfoRedisTemplate

use of com.rdbcache.configs.KeyInfoRedisTemplate in project rdbcache by rdbcache.

the class MockRedis method mockKeyInfoRedisTemplate.

public static KeyInfoRedisTemplate mockKeyInfoRedisTemplate() {
    KeyInfoRedisTemplate template = mock(KeyInfoRedisTemplate.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("KeyInfoRedisTemplate 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("KeyInfoRedisTemplate 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("KeyInfoRedisTemplate 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("KeyInfoRedisTemplate 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("KeyInfoRedisTemplate 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("KeyInfoRedisTemplate HashOperations delete " + key + " " + keys);
        for (String subKey : keys) {
            map.remove(subKey);
        }
        return null;
    }).when(keyInfoOps).delete(anyString(), anyList());
    return template;
}
Also used : KeyInfoRedisTemplate(com.rdbcache.configs.KeyInfoRedisTemplate) KeyInfo(com.rdbcache.models.KeyInfo) HashOperations(org.springframework.data.redis.core.HashOperations) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

KeyInfoRedisTemplate (com.rdbcache.configs.KeyInfoRedisTemplate)2 KeyInfo (com.rdbcache.models.KeyInfo)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 EventListener (org.springframework.context.event.EventListener)1 HashOperations (org.springframework.data.redis.core.HashOperations)1