Search in sources :

Example 1 with MockJedisCluster

use of com.fiftyonred.mock_jedis.MockJedisCluster in project druid by druid-io.

the class RedisClusterCacheTest method setUp.

@Before
public void setUp() {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(cacheConfig.getMaxTotalConnections());
    poolConfig.setMaxIdle(cacheConfig.getMaxIdleConnections());
    poolConfig.setMinIdle(cacheConfig.getMinIdleConnections());
    // orginal MockJedisCluster does not provide full support for all public get/set interfaces
    // some methods must be overriden for test cases
    cache = new RedisClusterCache(new MockJedisCluster(Collections.singleton(new HostAndPort("localhost", 6379))) {

        final ConcurrentHashMap<String, byte[]> cacheStorage = new ConcurrentHashMap<>();

        @Override
        public String setex(final byte[] key, final int seconds, final byte[] value) {
            cacheStorage.put(StringUtils.encodeBase64String(key), value);
            return null;
        }

        @Override
        public byte[] get(final byte[] key) {
            return cacheStorage.get(StringUtils.encodeBase64String(key));
        }

        @Override
        public List<byte[]> mget(final byte[]... keys) {
            mgetCount.incrementAndGet();
            List<byte[]> ret = new ArrayList<>();
            for (byte[] key : keys) {
                String k = StringUtils.encodeBase64String(key);
                byte[] value = cacheStorage.get(k);
                ret.add(value);
            }
            return ret;
        }
    }, cacheConfig);
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) MockJedisCluster(com.fiftyonred.mock_jedis.MockJedisCluster) Before(org.junit.Before)

Aggregations

MockJedisCluster (com.fiftyonred.mock_jedis.MockJedisCluster)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Before (org.junit.Before)1 HostAndPort (redis.clients.jedis.HostAndPort)1 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)1