Skip to content

Commit 2b1860b

Browse files
committed
Making it clear that the soft memory cache is a singleton
1 parent 83b9c09 commit 2b1860b

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/util/FlatFileCache.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,23 @@
2929

3030
import java.io.*;
3131

32-
/** Provides a cache for storing multiple small files in memory. Can be used to e.g cache gzip compressed PDB files for avoiding disk IO bottlenecks.
33-
*
32+
/**
33+
* Provides a cache for storing multiple small files in memory. Can be used to e.g cache gzip compressed PDB files
34+
* for avoiding disk IO bottlenecks.
35+
* Note this is just a wrapper for the singleton cache.
36+
*
3437
* @author Andreas Prlic.
3538
*
3639
*/
3740
public class FlatFileCache {
3841

3942
private final static Logger logger = LoggerFactory.getLogger(FlatFileCache.class);
4043

41-
private static FlatFileCache me ;
42-
44+
/**
45+
* The cache singleton.
46+
*/
4347
private static SoftHashMap<String, byte[]> cache = new SoftHashMap<String, byte[]>(0);
4448

45-
public static FlatFileCache getInstance() {
46-
47-
if ( me == null){
48-
me = new FlatFileCache();
49-
}
50-
51-
return me;
52-
}
5349

5450
// no public constructor;
5551
private FlatFileCache(){
@@ -109,20 +105,17 @@ public static InputStream getInputStream(String key){
109105

110106
}
111107

112-
public int size() {
108+
public static int size() {
113109
if ( cache != null)
114110
return cache.size();
115111
else
116112
return -1;
117113
}
118114

119-
public void clear(){
115+
public static void clear(){
120116
cache.clear();
121117
}
122118

123-
public static void destroy(){
124-
me.clear();
125-
me = null;
126-
}
119+
127120

128121
}

biojava-core/src/main/java/org/biojava/nbio/core/util/InputStreamProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public InputStreamProvider() {
7272
String prop = System.getProperty(CACHE_PROPERTY);
7373
if ( prop != null && prop.equals("true")) {
7474
cacheRawFiles = true;
75-
// init the singleton
76-
FlatFileCache.getInstance();
75+
7776
}
7877

7978
}

biojava-structure/src/main/java/org/biojava/nbio/structure/align/client/FarmJobRunnable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ protected void sendResultsToServer(List<String> results) {
611611
if ( progressListeners != null)
612612
notifySubmittingAlignments(results.size(), msg);
613613
logger.info("{}: Sent {} results to server. job status: {}", userName, results.size(), counter);
614-
logger.info("{}: fileCache size: {}", userName, FlatFileCache.getInstance().size());
614+
logger.info("{}: fileCache size: {}", userName, FlatFileCache.size());
615615
}
616616

617617

biojava-structure/src/test/java/org/biojava/nbio/structure/TestDownloadChemCompProvider.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public void testRedirectWorks() {
6969
file.delete();
7070

7171
// very important: we have a memory cache of files, we need to reset it not to pollute the cache for later tests
72-
FlatFileCache ffc = FlatFileCache.getInstance();
73-
ffc.clear();
72+
FlatFileCache.clear();
7473

7574
assertNotNull(cc);
7675

@@ -101,8 +100,7 @@ public void testWeDontCacheGarbage() {
101100
file.delete();
102101

103102
// very important: we have a memory cache of files, we need to reset it not to pollute the cache for later tests
104-
FlatFileCache ffc = FlatFileCache.getInstance();
105-
ffc.clear();
103+
FlatFileCache.clear();
106104

107105
// we couldn't parse, thus there should be no content
108106
assertNull(cc.getName());
@@ -116,6 +114,7 @@ public void testWeDontCacheGarbage() {
116114
@Test
117115
public void testIfWeCachedGarbageWeCanDetectIt() throws IOException {
118116
// see #703
117+
// TODO this test for the moment only asserts that we get an empty chemcomp, since we can't detect bad cached files yet
119118

120119
File file = new File(DownloadChemCompProvider.getLocalFileName("HEM"));
121120

@@ -132,8 +131,7 @@ public void testIfWeCachedGarbageWeCanDetectIt() throws IOException {
132131
file.delete();
133132

134133
// very important: we have a memory cache of files, we need to reset it not to pollute the cache for later tests
135-
FlatFileCache ffc = FlatFileCache.getInstance();
136-
ffc.clear();
134+
FlatFileCache.clear();
137135

138136
assertNull(cc.getName());
139137

0 commit comments

Comments
 (0)