Skip to content

Commit df591fc

Browse files
committed
Apps can now supply their own Cache implementations by supplying an injectable bean extending Internal.Cache. This is key to scalability as distributed caches can be supplied.
1 parent 10cb93a commit df591fc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main/java/com/hextremelabs/ussd/session/SessionManager.java

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.hextremelabs.ussd.session;
22

3+
import com.hextremelabs.ussd.internal.Internal.MapCache;
4+
5+
import javax.annotation.PostConstruct;
36
import javax.enterprise.context.ApplicationScoped;
7+
import javax.enterprise.inject.Any;
8+
import javax.enterprise.inject.Instance;
49
import javax.inject.Inject;
510
import javax.inject.Named;
611

@@ -25,6 +30,9 @@ public class SessionManager {
2530
private final String reverseQuery;
2631

2732
@Inject
33+
@Any
34+
private Instance<Cache> candidateCaches;
35+
2836
private Cache cache;
2937

3038
public SessionManager(){
@@ -38,6 +46,19 @@ public SessionManager(@Named("appName") String appName, Cache cache) {
3846
this.cache = cache;
3947
}
4048

49+
@PostConstruct
50+
private void setup() {
51+
Cache lastResort = null;
52+
for (Cache candidate : candidateCaches) {
53+
if (!(candidate instanceof MapCache)) {
54+
this.cache = candidate;
55+
return;
56+
}
57+
lastResort = candidate;
58+
}
59+
cache = lastResort;
60+
}
61+
4162
public Session putSession(Session session) throws CacheException {
4263
cache.put(session.getId(), session, DEFAULT_TENANT, appName, FIVE_MINUTES);
4364
cache.put(session.getMsisdn(), session, DEFAULT_TENANT, reverseQuery, FIVE_MINUTES);

0 commit comments

Comments
 (0)