RedisCacheStore implements CacheStore { @Override public <T> T get(String key, CacheContext<T> context) { try { byte[] value = redis.getBytes(key); if (value == null) return null; return deserialize(value, context.reader, context.validator); } catch (UncheckedIOException | RedisException e) { logger.warn(errorCode("CACHE_STORE_FAILED"), "failed to connect to redis, error={}", e.getMessage(), e); return null; } } 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 get() { when(redis.getBytes("key")).thenReturn(Strings.bytes("{\"stringField\":\"value\"}")); assertThat(cacheStore.get("key", context).stringField).isEqualTo("value"); }
@Test void getWithStaleData() { when(redis.getBytes("key")).thenReturn(Strings.bytes("{}")); assertThat(cacheStore.get("key", context)).isNull(); }
@Test void getWithInvalidJSON() { when(redis.getBytes("key")).thenReturn(Strings.bytes("{\"listField\": 1}")); assertThat(cacheStore.get("key", context)).isNull(); }
@Test void getWithFailure() { when(redis.getBytes("key")).thenThrow(new RedisException("unexpected")); assertThat(cacheStore.get("key", context)).isNull(); }