5050import java .util .stream .Collectors ;
5151
5252import ac .simons .oembed .OembedResponse .Format ;
53- import net .sf .ehcache .CacheManager ;
54- import net .sf .ehcache .Ehcache ;
5553import org .apache .commons .beanutils .BeanUtils ;
5654import org .apache .http .HttpResponse ;
5755import org .apache .http .HttpStatus ;
5856import org .apache .http .client .HttpClient ;
5957import org .apache .http .client .methods .HttpGet ;
6058import org .apache .http .util .EntityUtils ;
59+ import org .ehcache .Cache ;
60+ import org .ehcache .CacheManager ;
61+ import org .ehcache .config .builders .CacheConfigurationBuilder ;
62+ import org .ehcache .config .builders .ResourcePoolsBuilder ;
6163import org .jsoup .Jsoup ;
6264import org .jsoup .nodes .Document ;
6365import org .jsoup .nodes .Element ;
@@ -84,7 +86,7 @@ public class OembedService {
8486 /**
8587 * An optional cache manager used for caching oembed responses.
8688 */
87- private final Optional < CacheManager > cacheManager ;
89+ private final CacheManager cacheManager ;
8890
8991 /**
9092 * The user agent to use. We want to be a goot net citizen and provide some info about
@@ -123,11 +125,6 @@ public class OembedService {
123125 */
124126 private String cacheName = OembedService .class .getName ();
125127
126- /**
127- * Time in seconds responses are cached. Used if the response has no cache_age.
128- */
129- private long defaultCacheAge = 3600 ;
130-
131128 /**
132129 * Used for auto-discovered endpoints.
133130 */
@@ -138,6 +135,8 @@ public class OembedService {
138135 */
139136 private final OembedResponseRenderer defaultRenderer = new DefaultOembedResponseRenderer ();
140137
138+ private final OembedResponseExpiryPolicy expiryPolicy = new OembedResponseExpiryPolicy ();
139+
141140 /**
142141 * Creates a new {@code OembedService}. This service depends on a {@link HttpClient}
143142 * and can use a {@link CacheManager} for caching requests.
@@ -149,7 +148,7 @@ public class OembedService {
149148 public OembedService (final HttpClient httpClient , final CacheManager cacheManager ,
150149 final List <OembedEndpoint > endpoints , final String applicationName ) {
151150 this .httpClient = httpClient ;
152- this .cacheManager = Optional . ofNullable ( cacheManager ) ;
151+ this .cacheManager = cacheManager ;
153152 final Properties version = new Properties ();
154153 try {
155154 version .load (OembedService .class .getResourceAsStream ("/oembed.properties" ));
@@ -236,8 +235,9 @@ public String getCacheName() {
236235 * @param cacheName the new cache name
237236 */
238237 public void setCacheName (final String cacheName ) {
239- if (this .cacheManager .isPresent () && this .cacheManager .get ().cacheExists (this .cacheName )) {
240- this .cacheManager .get ().removeCache (this .cacheName );
238+ if (this .cacheManager != null
239+ && this .cacheManager .getCache (this .cacheName , String .class , OembedResponseWrapper .class ) != null ) {
240+ this .cacheManager .removeCache (this .cacheName );
241241 }
242242 this .cacheName = cacheName ;
243243 }
@@ -246,15 +246,15 @@ public void setCacheName(final String cacheName) {
246246 * {@return the default time in seconds responses are cached}
247247 */
248248 public long getDefaultCacheAge () {
249- return this .defaultCacheAge ;
249+ return this .expiryPolicy . getDefaultCacheAge () ;
250250 }
251251
252252 /**
253253 * Changes the default cache age.
254254 * @param defaultCacheAge new default cache age in seconds
255255 */
256256 public void setDefaultCacheAge (final long defaultCacheAge ) {
257- this .defaultCacheAge = defaultCacheAge ;
257+ this .expiryPolicy . setDefaultCacheAge ( defaultCacheAge ) ;
258258 }
259259
260260 /**
@@ -331,6 +331,26 @@ final InputStream executeRequest(final HttpGet request) {
331331 return rv ;
332332 }
333333
334+ /**
335+ * Gets or creates the cache for oembed responses. In Ehcache 3.x, caches need to be
336+ * explicitly created with a configuration.
337+ * @return the cache instance or null if there is no cache manager
338+ */
339+ private Cache <String , OembedResponseWrapper > getOrCreateCache () {
340+ if (this .cacheManager == null ) {
341+ return null ;
342+ }
343+ var cache = this .cacheManager .getCache (this .cacheName , String .class , OembedResponseWrapper .class );
344+ if (cache != null ) {
345+ return cache ;
346+ }
347+
348+ return this .cacheManager .createCache (this .cacheName , CacheConfigurationBuilder
349+ .newCacheConfigurationBuilder (String .class , OembedResponseWrapper .class , ResourcePoolsBuilder .heap (1000 ))
350+ .withExpiry (new OembedResponseExpiryPolicy ())
351+ .build ());
352+ }
353+
334354 /**
335355 * Tries to find an {@link OembedResponse} for the URL {@code url}. If a cache manager
336356 * is present, it tries that first. If an {@code OembedResponse} can be discovered and
@@ -345,8 +365,9 @@ public Optional<OembedResponse> getOembedResponseFor(final String url) {
345365 return Optional .empty ();
346366 }
347367
348- var rv = this .cacheManager .map (cm -> cm .addCacheIfAbsent (this .cacheName ).get (trimmedUrl ))
349- .map (element -> (OembedResponse ) element .getObjectValue ());
368+ var rv = Optional .ofNullable (this .getOrCreateCache ())
369+ .map (cache -> cache .get (trimmedUrl ))
370+ .map (OembedResponseWrapper ::value );
350371 // If there's already an oembed response cached, use that
351372 if (rv .isPresent ()) {
352373 LOGGER .debug ("Using OembedResponse from cache for '{}'..." , trimmedUrl );
@@ -371,15 +392,12 @@ public Optional<OembedResponse> getOembedResponseFor(final String url) {
371392 return oembedResponse ;
372393 });
373394
374- if (this .cacheManager .isPresent ()) {
375- final Ehcache cache = this .cacheManager .get ().addCacheIfAbsent (this .cacheName );
376- // Cache at least 60 seconds
377- final int cacheAge = (int ) Math .min (
378- Math .max (60L , rv .map (OembedResponse ::getCacheAge ).orElse (this .defaultCacheAge )), Integer .MAX_VALUE );
395+ if (this .cacheManager != null ) {
396+ var cache = getOrCreateCache ();
379397 // We're adding failed urls to the cache as well to prevent them
380398 // from being tried again over and over (at least for some seconds)
381- cache .put (new net . sf . ehcache . Element ( trimmedUrl , rv .orElse (null ), cacheAge , cacheAge ));
382- LOGGER .debug ("Cached {} for {} seconds for url '{}'..." , rv , cacheAge , trimmedUrl );
399+ cache .put (trimmedUrl , new OembedResponseWrapper ( rv .orElse (null )));
400+ LOGGER .debug ("Cached response {} from url '{}'..." , rv , trimmedUrl );
383401 }
384402
385403 return rv ;
0 commit comments