@Test public void shouldReuseEncodedCaches() { EmbeddedCacheManager embeddedCacheManager = Mockito.spy(cacheManager); RestCacheManager<Object> restCacheManager = new RestCacheManager<>(embeddedCacheManager, c -> Boolean.FALSE); Map<String, Cache<String, ?>> knownCaches = TestingUtil.extractField(restCacheManager, "knownCaches"); SimpleRequest request = new SimpleRequest.Builder().setPath("/test").build(); restCacheManager.getCache("cache1", request); restCacheManager.getCache("cache2", request); assertEquals(knownCaches.size(), 2); Mockito.reset(embeddedCacheManager); restCacheManager.getCache("cache1", request); restCacheManager.getCache("cache2", request); Mockito.verify(embeddedCacheManager, never()).getCache("cache1"); Mockito.verify(embeddedCacheManager, never()).getCache("cache2"); restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.APPLICATION_JSON, request); restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.TEXT_PLAIN, request); assertEquals(knownCaches.size(), 4); Mockito.reset(embeddedCacheManager); restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.TEXT_PLAIN, request); restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.fromString("text/plain; charset=UTF-8"), request); restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.fromString("text/plain; charset=SHIFT-JIS"), request); assertEquals(knownCaches.keySet().size(), 4); Mockito.verify(embeddedCacheManager, never()).getCache("cache1"); Mockito.verify(embeddedCacheManager, never()).getCache("cache2"); }
@SuppressWarnings("unchecked") 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); String cacheKey = name + "-" + keyContentType.getTypeSubtype() + valueContentType.getTypeSubtype(); AdvancedCache<Object, V> cache = knownCaches.get(cacheKey); if (cache == null) { cache = instance.<Object, V>getCache(name).getAdvancedCache(); tryRegisterMigrationManager(cache); cache = (AdvancedCache<Object, V>) cache.getAdvancedCache() .withMediaType(keyContentType.toString(), valueContentType.toString()) .withFlags(IGNORE_RETURN_VALUES); knownCaches.putIfAbsent(cacheKey, cache); } if (flags != null && flags.length > 0) cache = cache.withFlags(flags); return subject == null ? cache : cache.withSubject(subject); }
RestCacheManager { @SuppressWarnings("unchecked") 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); String cacheKey = name + "-" + keyContentType.getTypeSubtype() + valueContentType.getTypeSubtype(); AdvancedCache<Object, V> cache = knownCaches.get(cacheKey); if (cache == null) { cache = instance.<Object, V>getCache(name).getAdvancedCache(); tryRegisterMigrationManager(cache); cache = (AdvancedCache<Object, V>) cache.getAdvancedCache() .withMediaType(keyContentType.toString(), valueContentType.toString()) .withFlags(IGNORE_RETURN_VALUES); knownCaches.putIfAbsent(cacheKey, cache); } if (flags != null && flags.length > 0) cache = cache.withFlags(flags); return subject == null ? cache : cache.withSubject(subject); } }
RestCacheManager { @SuppressWarnings("unchecked") 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); String cacheKey = name + "-" + keyContentType.getTypeSubtype() + valueContentType.getTypeSubtype(); AdvancedCache<Object, V> cache = knownCaches.get(cacheKey); if (cache == null) { cache = instance.<Object, V>getCache(name).getAdvancedCache(); tryRegisterMigrationManager(cache); cache = (AdvancedCache<Object, V>) cache.getAdvancedCache() .withMediaType(keyContentType.toString(), valueContentType.toString()) .withFlags(IGNORE_RETURN_VALUES); knownCaches.putIfAbsent(cacheKey, cache); } if (flags != null && flags.length > 0) cache = cache.withFlags(flags); return subject == null ? cache : cache.withSubject(subject); } RestCacheManager(EmbeddedCacheManager instance, Predicate<? super String> isCacheIgnored); }
RestCacheManager { @SuppressWarnings("unchecked") 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); String cacheKey = name + "-" + keyContentType.getTypeSubtype() + valueContentType.getTypeSubtype(); AdvancedCache<Object, V> cache = knownCaches.get(cacheKey); if (cache == null) { cache = instance.<Object, V>getCache(name).getAdvancedCache(); tryRegisterMigrationManager(cache); cache = (AdvancedCache<Object, V>) cache.getAdvancedCache() .withMediaType(keyContentType.toString(), valueContentType.toString()) .withFlags(IGNORE_RETURN_VALUES); knownCaches.putIfAbsent(cacheKey, cache); } if (flags != null && flags.length > 0) cache = cache.withFlags(flags); return subject == null ? cache : cache.withSubject(subject); } RestCacheManager(EmbeddedCacheManager instance, Predicate<? super String> isCacheIgnored); @SuppressWarnings("unchecked") AdvancedCache<Object, V> getCache(String name, MediaType keyContentType, MediaType valueContentType, RestRequest request); AdvancedCache<Object, V> getCache(String name, RestRequest restRequest); boolean cacheExists(String name); boolean isCacheQueryable(Cache<?, ?> cache); Collection<String> getCacheNames(); CompletionStage<CacheEntry<Object, V>> getInternalEntry(String cacheName, Object key, MediaType keyContentType, MediaType mediaType, RestRequest request); CompletionStage<V> remove(String cacheName, Object key, MediaType keyContentType, RestRequest restRequest); CompletionStage<CacheEntry<Object, V>> getPrivilegedInternalEntry(AdvancedCache<Object, V> cache, Object key, boolean skipListener); MediaType getValueConfiguredFormat(String cacheName, RestRequest restRequest); String getNodeName(); String getServerAddress(); String getPrimaryOwner(String cacheName, Object key, RestRequest restRequest); String getBackupOwners(String cacheName, Object key, RestRequest restRequest); EmbeddedCacheManager getInstance(); EmbeddedCacheManagerAdmin getCacheManagerAdmin(RestRequest restRequest); void stop(); }
RestCacheManager { @SuppressWarnings("unchecked") 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); String cacheKey = name + "-" + keyContentType.getTypeSubtype() + valueContentType.getTypeSubtype(); AdvancedCache<Object, V> cache = knownCaches.get(cacheKey); if (cache == null) { cache = instance.<Object, V>getCache(name).getAdvancedCache(); tryRegisterMigrationManager(cache); cache = (AdvancedCache<Object, V>) cache.getAdvancedCache() .withMediaType(keyContentType.toString(), valueContentType.toString()) .withFlags(IGNORE_RETURN_VALUES); knownCaches.putIfAbsent(cacheKey, cache); } if (flags != null && flags.length > 0) cache = cache.withFlags(flags); return subject == null ? cache : cache.withSubject(subject); } RestCacheManager(EmbeddedCacheManager instance, Predicate<? super String> isCacheIgnored); @SuppressWarnings("unchecked") AdvancedCache<Object, V> getCache(String name, MediaType keyContentType, MediaType valueContentType, RestRequest request); AdvancedCache<Object, V> getCache(String name, RestRequest restRequest); boolean cacheExists(String name); boolean isCacheQueryable(Cache<?, ?> cache); Collection<String> getCacheNames(); CompletionStage<CacheEntry<Object, V>> getInternalEntry(String cacheName, Object key, MediaType keyContentType, MediaType mediaType, RestRequest request); CompletionStage<V> remove(String cacheName, Object key, MediaType keyContentType, RestRequest restRequest); CompletionStage<CacheEntry<Object, V>> getPrivilegedInternalEntry(AdvancedCache<Object, V> cache, Object key, boolean skipListener); MediaType getValueConfiguredFormat(String cacheName, RestRequest restRequest); String getNodeName(); String getServerAddress(); String getPrimaryOwner(String cacheName, Object key, RestRequest restRequest); String getBackupOwners(String cacheName, Object key, RestRequest restRequest); EmbeddedCacheManager getInstance(); EmbeddedCacheManagerAdmin getCacheManagerAdmin(RestRequest restRequest); void stop();  }