|
12 | 12 |
|
13 | 13 | import com.google.common.cache.Cache;
|
14 | 14 | import com.google.common.cache.CacheBuilder;
|
| 15 | +import com.google.common.cache.RemovalListener; |
15 | 16 | import org.apache.commons.math3.util.Pair;
|
16 | 17 | import org.obiba.magma.NoSuchValueTableException;
|
| 18 | +import org.obiba.magma.support.Disposables; |
17 | 19 | import org.obiba.mica.core.source.ExcelTableSource;
|
18 | 20 | import org.obiba.mica.core.source.OpalTableSource;
|
19 | 21 | import org.obiba.mica.dataset.domain.StudyDataset;
|
|
29 | 31 | import org.slf4j.LoggerFactory;
|
30 | 32 | import org.springframework.stereotype.Service;
|
31 | 33 |
|
| 34 | +import javax.annotation.PostConstruct; |
| 35 | +import javax.annotation.PreDestroy; |
32 | 36 | import javax.inject.Inject;
|
33 | 37 | import java.io.InputStream;
|
34 | 38 | import java.util.NoSuchElementException;
|
@@ -56,12 +60,28 @@ public class StudyTableSourceServiceRegistry {
|
56 | 60 | @Inject
|
57 | 61 | private FileStoreService fileStoreService;
|
58 | 62 |
|
59 |
| - private Cache<String, StudyTableSource> sourcesCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES).build(); |
| 63 | + private Cache<String, StudyTableSource> sourcesCache; |
| 64 | + |
| 65 | + @PostConstruct |
| 66 | + public void initialize() { |
| 67 | + sourcesCache = CacheBuilder.newBuilder() |
| 68 | + .maximumSize(1000) |
| 69 | + .expireAfterWrite(1, TimeUnit.MINUTES) |
| 70 | + .removalListener((RemovalListener<String, StudyTableSource>) notification -> { |
| 71 | + Disposables.silentlyDispose(notification.getValue()); |
| 72 | + }) |
| 73 | + .build(); |
| 74 | + } |
| 75 | + |
| 76 | + @PreDestroy |
| 77 | + public void close() { |
| 78 | + sourcesCache.cleanUp(); |
| 79 | + } |
60 | 80 |
|
61 | 81 | public synchronized StudyTableSource makeStudyTableSource(IDataset dataset, IStudy study, String source) {
|
62 | 82 | StudyTableContext context = new StudyTableContext(dataset, study, micaConfigService.getConfig().getPrivacyThreshold());
|
63 | 83 |
|
64 |
| - String cacheKey = String.format("%s::%s", study.getId(), source); |
| 84 | + String cacheKey = String.format("%s::%s::%s", dataset.getId(), study.getId(), source); |
65 | 85 | try {
|
66 | 86 | return sourcesCache.get(cacheKey, () -> makeStudyTableSourceInternal(context, source));
|
67 | 87 | } catch (ExecutionException e) {
|
|
0 commit comments