public AdvancedCache<Object, V> getCache(String name, MediaType keyContentType, MediaType valueContentType, RestRequest request) {
      Subject subject = request.getSubject();
      Flag[] flags = request.getFlags();
      if (isCacheIgnored.test(name)) {
         throw logger.cacheUnavailable(name);
      }
      if (keyContentType == null || valueContentType == null) {
         throw logger.missingRequiredMediaType(name);
      }
      checkCacheAvailable(name);
      CacheInfo<Object, V> cacheInfo = knownCaches.get(name);
      if (cacheInfo == null) {
         AdvancedCache<Object, V> cache = instance.<Object, V>getCache(name).getAdvancedCache()
               .withFlags(Flag.IGNORE_RETURN_VALUES);
         cacheInfo = new CacheInfo<>(cache);
         knownCaches.putIfAbsent(name, cacheInfo);
      }
      AdvancedCache<Object, V> cache = cacheInfo.getCache(new KeyValuePair<>(keyContentType, valueContentType), subject);
      if (flags != null && flags.length > 0) cache = cache.withFlags(flags);
      return cache;
   }
@Test
   public void shouldReuseEncodedCaches() {
      EmbeddedCacheManager embeddedCacheManager = Mockito.spy(cacheManager);
      RestCacheManager<Object> restCacheManager = new RestCacheManager<>(embeddedCacheManager, c -> Boolean.FALSE);
      Map<String, CacheInfo<Object, Object>> knownCaches = TestingUtil.extractField(restCacheManager, "knownCaches");

      // Request cache by simple name
      SimpleRequest request = new SimpleRequest.Builder().setPath("/test").build();
      restCacheManager.getCache("cache1", request);
      restCacheManager.getCache("cache2", request);

      // Verify they are stored internally
      assertEquals(knownCaches.size(), 2);
      assertEquals(cachesSize(knownCaches.get("cache1")), 1);
      assertEquals(cachesSize(knownCaches.get("cache2")), 1);

      // Requesting again should not cause interaction with the cache manager
      Mockito.reset(embeddedCacheManager);
      restCacheManager.getCache("cache1", request);
      restCacheManager.getCache("cache2", request);

      Mockito.verify(embeddedCacheManager, never()).getCache("cache1");
      Mockito.verify(embeddedCacheManager, never()).getCache("cache2");
      assertEquals(cachesSize(knownCaches.get("cache1")), 1);
      assertEquals(cachesSize(knownCaches.get("cache2")), 1);

      // Request caches with a different media type
      restCacheManager.getCache("cache1", MediaType.MATCH_ALL, MediaType.APPLICATION_JSON, request);
      restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.TEXT_PLAIN, request);

      // Verify they are stored internally
      assertEquals(knownCaches.size(), 2);
      assertEquals(cachesSize(knownCaches.get("cache1")), 2);
      assertEquals(cachesSize(knownCaches.get("cache2")), 2);

      // Requesting again with same media type but different parameters should not reuse internal instance
      Mockito.reset(embeddedCacheManager);
      restCacheManager.getCache("cache1", MediaType.MATCH_ALL, MediaType.fromString("application/json; charset=UTF-8"), request);
      restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.fromString("text/plain; charset=SHIFT-JIS"), request);

      assertEquals(knownCaches.size(), 2);
      assertEquals(cachesSize(knownCaches.get("cache1")), 3);
      assertEquals(cachesSize(knownCaches.get("cache2")), 3);
      Mockito.verify(embeddedCacheManager, never()).getCache("cache1");
      Mockito.verify(embeddedCacheManager, never()).getCache("cache2");

      // Requesting with same params should reuse
      restCacheManager.getCache("cache1", MediaType.MATCH_ALL, MediaType.fromString("application/json; charset=UTF-8"), request);
      restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.fromString("text/plain; charset=SHIFT-JIS"), request);

      assertEquals(cachesSize(knownCaches.get("cache1")), 3);
      assertEquals(cachesSize(knownCaches.get("cache2")), 3);
      Mockito.verify(embeddedCacheManager, never()).getCache("cache1");
      Mockito.verify(embeddedCacheManager, never()).getCache("cache2");
   }