mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
do not cache resource urls in development stage, to avoid stale data
This commit is contained in:
@@ -41,6 +41,8 @@ import com.google.common.collect.ImmutableList;
|
|||||||
import com.google.common.collect.ImmutableList.Builder;
|
import com.google.common.collect.ImmutableList.Builder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import sonia.scm.SCMContext;
|
||||||
|
import sonia.scm.Stage;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@@ -69,19 +71,21 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
|||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
public DefaultUberWebResourceLoader(ServletContext servletContext, Iterable<PluginWrapper> plugins) {
|
||||||
* Constructs ...
|
this(servletContext, plugins, SCMContext.getContext().getStage());
|
||||||
*
|
}
|
||||||
*
|
|
||||||
* @param servletContext
|
public DefaultUberWebResourceLoader(ServletContext servletContext, Iterable<PluginWrapper> plugins, Stage stage) {
|
||||||
* @param plugins
|
|
||||||
*/
|
|
||||||
public DefaultUberWebResourceLoader(ServletContext servletContext,
|
|
||||||
Iterable<PluginWrapper> plugins)
|
|
||||||
{
|
|
||||||
this.servletContext = servletContext;
|
this.servletContext = servletContext;
|
||||||
this.plugins = plugins;
|
this.plugins = plugins;
|
||||||
this.cache = CacheBuilder.newBuilder().build();
|
this.cache = createCache(stage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Cache<String, URL> createCache(Stage stage) {
|
||||||
|
if (stage == Stage.DEVELOPMENT) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return CacheBuilder.newBuilder().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
@@ -97,7 +101,7 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
|||||||
@Override
|
@Override
|
||||||
public URL getResource(String path)
|
public URL getResource(String path)
|
||||||
{
|
{
|
||||||
URL resource = cache.getIfPresent(path);
|
URL resource = getFromCache(path);
|
||||||
|
|
||||||
if (resource == null)
|
if (resource == null)
|
||||||
{
|
{
|
||||||
@@ -105,7 +109,7 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
|||||||
|
|
||||||
if (resource != null)
|
if (resource != null)
|
||||||
{
|
{
|
||||||
cache.put(path, resource);
|
addToCache(path, resource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -116,6 +120,19 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
|||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private URL getFromCache(String path) {
|
||||||
|
if (cache != null) {
|
||||||
|
return cache.getIfPresent(path);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addToCache(String path, URL url) {
|
||||||
|
if (cache != null) {
|
||||||
|
cache.put(path, url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import sonia.scm.Stage;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -96,14 +97,12 @@ public class DefaultUberWebResourceLoaderTest extends WebResourceLoaderTestBase
|
|||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @throws MalformedURLException
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGetResourceFromCache() throws MalformedURLException
|
public void testGetResourceFromCache() {
|
||||||
{
|
|
||||||
DefaultUberWebResourceLoader resourceLoader =
|
DefaultUberWebResourceLoader resourceLoader =
|
||||||
new DefaultUberWebResourceLoader(servletContext,
|
new DefaultUberWebResourceLoader(servletContext,
|
||||||
new ArrayList<PluginWrapper>());
|
new ArrayList<PluginWrapper>(), Stage.PRODUCTION);
|
||||||
|
|
||||||
resourceLoader.getCache().put("/myresource", GITHUB);
|
resourceLoader.getCache().put("/myresource", GITHUB);
|
||||||
|
|
||||||
@@ -112,6 +111,15 @@ public class DefaultUberWebResourceLoaderTest extends WebResourceLoaderTestBase
|
|||||||
assertSame(GITHUB, resource);
|
assertSame(GITHUB, resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetResourceCacheIsDisableInStageDevelopment() throws MalformedURLException {
|
||||||
|
DefaultUberWebResourceLoader resourceLoader = new DefaultUberWebResourceLoader(servletContext, new ArrayList<>(), Stage.DEVELOPMENT);
|
||||||
|
when(servletContext.getResource("/scm")).thenAnswer(invocation -> new URL("https://scm-manager.org"));
|
||||||
|
URL url = resourceLoader.getResource("/scm");
|
||||||
|
URL secondUrl = resourceLoader.getResource("/scm");
|
||||||
|
assertNotSame(url, secondUrl);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user