RedisCacheStore implements CacheStore { @Override public <T> Map<String, T> getAll(String[] keys, CacheContext<T> context) { try { Map<String, byte[]> redisValues = redis.multiGetBytes(keys); Map<String, T> values = Maps.newHashMapWithExpectedSize(redisValues.size()); for (Map.Entry<String, byte[]> entry : redisValues.entrySet()) { T value = deserialize(entry.getValue(), context.reader, context.validator); if (value != null) { values.put(entry.getKey(), value); } } return values; } catch (UncheckedIOException | RedisException e) { logger.warn(errorCode("CACHE_STORE_FAILED"), "failed to connect to redis, error={}", e.getMessage(), e); return Map.of(); } } RedisCacheStore(RedisImpl redis); @Override T get(String key, CacheContext<T> context); @Override Map<String, T> getAll(String[] keys, CacheContext<T> context); @Override void put(String key, T value, Duration expiration, CacheContext<T> context); @Override void putAll(List<Entry<T>> values, Duration expiration, CacheContext<T> context); @Override boolean delete(String... keys);  }
@Test void getAll() { Map<String, byte[]> values = Map.of("key", Strings.bytes("{\"stringField\":\"value\"}")); when(redis.multiGetBytes("key")).thenReturn(values); Map<String, TestCache> results = cacheStore.getAll(new String[]{"key"}, context); assertThat(results).hasSize(1); assertThat(results.get("key").stringField).isEqualTo("value"); }
@Test void getAllWithFailure() { when(redis.multiGetBytes("key")).thenThrow(new RedisException("unexpected")); assertThat(cacheStore.getAll(new String[]{"key"}, context)).isEmpty(); }