Skip to content

Commit ba480a6

Browse files
committed
Inject Issue in Cache modules and MVC fix #573
1 parent 3ede008 commit ba480a6

4 files changed

Lines changed: 117 additions & 6 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.jooby.issues;
2+
3+
import java.util.concurrent.ExecutionException;
4+
5+
import javax.inject.Inject;
6+
7+
import org.jooby.guava.GuavaCache;
8+
import org.jooby.guava.GuavaSessionStore;
9+
import org.jooby.mvc.GET;
10+
import org.jooby.mvc.Path;
11+
import org.jooby.test.ServerFeature;
12+
import org.junit.Test;
13+
14+
import com.google.common.cache.Cache;
15+
import com.typesafe.config.ConfigFactory;
16+
import com.typesafe.config.ConfigValueFactory;
17+
18+
public class Issue573 extends ServerFeature {
19+
20+
@Path("/573")
21+
public static class MvcRoute {
22+
23+
private Cache<String, Object> cache;
24+
25+
@Inject
26+
public MvcRoute(final Cache<String, Object> cache) {
27+
this.cache = cache;
28+
}
29+
30+
@GET
31+
public Object get() throws ExecutionException {
32+
return cache.get("foo", () -> "bar");
33+
}
34+
}
35+
36+
{
37+
use(ConfigFactory.empty()
38+
.withValue("guava.cache", ConfigValueFactory.fromAnyRef("maximumSize=10"))
39+
.withValue("guava.session", ConfigValueFactory.fromAnyRef("maximumSize=10")));
40+
41+
use(GuavaCache.newCache());
42+
43+
session(GuavaSessionStore.class);
44+
45+
use(MvcRoute.class);
46+
}
47+
48+
@Test
49+
public void shouldInjectCacheIntoMvcRoute() throws Exception {
50+
request()
51+
.get("/573")
52+
.expect("bar");
53+
}
54+
55+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.jooby.issues;
2+
3+
import java.util.concurrent.ExecutionException;
4+
5+
import javax.inject.Inject;
6+
7+
import org.jooby.guava.GuavaCache;
8+
import org.jooby.guava.GuavaSessionStore;
9+
import org.jooby.mvc.GET;
10+
import org.jooby.mvc.Path;
11+
import org.jooby.test.ServerFeature;
12+
import org.junit.Test;
13+
14+
import com.google.common.cache.Cache;
15+
import com.typesafe.config.ConfigFactory;
16+
import com.typesafe.config.ConfigValueFactory;
17+
18+
public class Issue573b extends ServerFeature {
19+
20+
@Path("/573")
21+
public static class MvcRoute {
22+
23+
private Cache<String, Object> cache;
24+
25+
@Inject
26+
public MvcRoute(final Cache<String, Object> cache) {
27+
this.cache = cache;
28+
}
29+
30+
@GET
31+
public Object get() throws ExecutionException {
32+
return cache.get("foo", () -> "bar");
33+
}
34+
}
35+
36+
{
37+
use(ConfigFactory.empty()
38+
.withValue("guava.session", ConfigValueFactory.fromAnyRef("maximumSize=10")));
39+
40+
use(GuavaCache.newCache());
41+
42+
session(GuavaSessionStore.class);
43+
44+
use(MvcRoute.class);
45+
}
46+
47+
@Test
48+
public void shouldInjectCacheIntoMvcRoute() throws Exception {
49+
request()
50+
.get("/573")
51+
.expect("bar");
52+
}
53+
54+
}

jooby-caffeine/src/main/java/org/jooby/caffeine/CaffeineCache.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ public CaffeineCache<K, V> doWithAsync(final AsyncCallback<K, V> configurer) {
242242
@SuppressWarnings({"unchecked", "rawtypes" })
243243
@Override
244244
public void configure(final Env env, final Config conf, final Binder binder) {
245-
Config gconf = conf.hasPath(name)
246-
? conf
247-
: ConfigFactory.empty().withValue("caffeine.cache", ConfigValueFactory.fromAnyRef(""));
245+
Config gconf = conf.hasPath(name) ? conf : ConfigFactory.empty();
246+
247+
gconf = gconf.withFallback(
248+
ConfigFactory.empty().withValue("caffeine.cache", ConfigValueFactory.fromAnyRef("")));
248249

249250
gconf.getObject(name).unwrapped().forEach((name, spec) -> {
250251
Caffeine<K, V> cb = (Caffeine<K, V>) Caffeine.from(toSpec(spec));

jooby-guava-cache/src/main/java/org/jooby/guava/GuavaCache.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ public GuavaCache<K, V> doWith(final Callback<K, V, Cache<K, V>> configurer) {
222222
@SuppressWarnings({"unchecked", "rawtypes" })
223223
@Override
224224
public void configure(final Env env, final Config conf, final Binder binder) {
225-
Config gconf = conf.hasPath(name)
226-
? conf
227-
: ConfigFactory.empty().withValue("guava.cache", ConfigValueFactory.fromAnyRef(""));
225+
Config gconf = conf.hasPath(name) ? conf : ConfigFactory.empty();
226+
227+
gconf = gconf.withFallback(
228+
ConfigFactory.empty().withValue("guava.cache", ConfigValueFactory.fromAnyRef("")));
228229

229230
gconf.getObject(name).unwrapped().forEach((name, spec) -> {
230231
CacheBuilder<K, V> cb = (CacheBuilder<K, V>) CacheBuilder.from(toSpec(spec));

0 commit comments

Comments
 (0)