Content of file DefaultMetadataCache.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>DefaultMetadataCache.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">NorthernWind :: Frontend :: Media :: Spring MVC</a> > <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-frontend-media</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.media.impl</a> > <span class="el_source">DefaultMetadataCache.java</span></div><h1>DefaultMetadataCache.java</h1><pre class="source lang-java linenums">/*
* #%L
* *********************************************************************************************************************
*
* NorthernWind - lightweight CMS
* http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
* %%
* Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
* %%
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
*
* *********************************************************************************************************************
* #L%
*/
package it.tidalwave.northernwind.frontend.media.impl;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.time.Clock;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.io.IOException;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.northernwind.core.model.ResourceProperties;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* A default implementation of {@link MetadataCache}.
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="fc" id="L54">@Slf4j</span>
<span class="fc" id="L55">public class DefaultMetadataCache implements MetadataCache</span>
{
/*******************************************************************************************************************
*
* A holder of {@code Metadata} together with expiration information.
*
******************************************************************************************************************/
<span class="pc" id="L62"> @RequiredArgsConstructor @Getter @ToString</span>
class ExpirableMetadata
{
@Nonnull
<span class="fc" id="L66"> private final Metadata metadata;</span>
<span class="fc" id="L68"> private final ZonedDateTime creationTime = ZonedDateTime.now(clock.get());</span>
<span class="fc" id="L70"> @Nonnull</span>
<span class="fc" id="L71"> private ZonedDateTime expirationTime = creationTime.plusSeconds(metadataExpirationTime);</span>
/***************************************************************************************************************
*
* Postpones the expiration time.
*
**************************************************************************************************************/
public void postponeExpirationTime()
{
<span class="fc" id="L80"> expirationTime = ZonedDateTime.now(clock.get()).plusSeconds(metadataExpirationTime);</span>
<span class="fc" id="L81"> }</span>
}
public static final int DEFAULT_METADATA_EXPIRATION_TIME = 10 * 60;
<span class="pc" id="L86"> @Getter @Setter @Nonnull</span>
private Supplier<Clock> clock = Clock::systemDefaultZone;
/** Expiration time for metadata in seconds; after this time, metadata are reloaded. */
<span class="pc" id="L90"> @Getter @Setter @Nonnegative</span>
private int metadataExpirationTime = DEFAULT_METADATA_EXPIRATION_TIME;
@Inject
private MetadataLoader metadataLoader;
<span class="fc" id="L96"> /* package */ final Map<Id, ExpirableMetadata> metadataMapById = new HashMap<>();</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
// FIXME: shouldn't synchronize the whole method, only map manipulation
| shouldn't synchronize the whole method, only map manipulation | |
@Override @Nonnull
public synchronized Metadata findMetadataById (@Nonnull final Id mediaId,
@Nonnull final ResourceProperties siteNodeProperties)
throws NotFoundException, IOException
{
<span class="fc" id="L109"> log.debug("findMetadataById({}, ...)", mediaId);</span>
<span class="fc" id="L110"> var metadata = metadataMapById.get(mediaId);</span>
<span class="fc bfc" id="L112" title="All 4 branches covered."> if ((metadata != null) && metadata.getExpirationTime().isAfter(ZonedDateTime.now(clock.get())))</span>
{
<span class="fc" id="L114"> log.debug(">>>> returning cached data which will expire at {}", metadata.getExpirationTime());</span>
<span class="fc" id="L115"> return metadata.getMetadata();</span>
}
<span class="fc" id="L118"> final var file = metadataLoader.findMediaResourceFile(siteNodeProperties, mediaId);</span>
<span class="fc bfc" id="L120" title="All 2 branches covered."> if (metadata != null)</span>
{
<span class="fc" id="L122"> final var fileLatestModificationTime = file.getLatestModificationTime();</span>
<span class="fc" id="L123"> final var metadataCreationTime = metadata.getCreationTime();</span>
<span class="fc bfc" id="L125" title="All 2 branches covered."> if (fileLatestModificationTime.isAfter(metadataCreationTime))</span>
{
<span class="fc" id="L127"> log.debug(">>>>>>>> expiring metadata: file {} > metadata {}",</span>
fileLatestModificationTime, metadataCreationTime);
<span class="fc" id="L129"> metadata = null;</span>
}
else
{
<span class="fc" id="L133"> log.debug(">>>>>>>> postponing metadata expiration: file {} < metadata {}",</span>
fileLatestModificationTime, metadataCreationTime);
<span class="fc" id="L135"> metadata.postponeExpirationTime();</span>
}
}
<span class="fc bfc" id="L139" title="All 2 branches covered."> if (metadata == null)</span>
{
<span class="fc" id="L141"> log.debug(">>>> loading metadata...");</span>
<span class="fc" id="L142"> metadata = new ExpirableMetadata(metadataLoader.loadMetadata(file));</span>
<span class="fc" id="L143"> metadataMapById.put(mediaId, metadata);</span>
}
<span class="fc" id="L146"> return metadata.getMetadata();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>