Skip to content

Commit c61fcb8

Browse files
committed
🎨 Rename cache control properties, and better tolerate cache creation failures
1 parent 5b05380 commit c61fcb8

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ There are a couple of static classes that help common usage scenarios:
4040
`Spdx-Java-Library` is configured using Java system properties, as those allow both human operator configuration (via JVM `-D` command line options) as well as programmatic configuration by downstream code that needs automated control.
4141

4242
Currently the library offers these configuration options:
43-
1. `org.spdx.storage.listedlicense.SpdxListedLicenseWebStore.enableCache` - a boolean that enables or disables the WebStore's local cache. Defaults to `false` (the cache is disabled). The cache location is determined as per the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) i.e. `${XDG_CACHE_HOME}/Spdx-Java-Library` or `${HOME}/.cache/Spdx-Java-Library`).
44-
2. `org.spdx.storage.listedlicense.SpdxListedLicenseWebStore.cacheCheckIntervalSecs` - a long that controls how often each cache entry is rechecked for staleness, in units of seconds. Defaults to 86,400 seconds (24 hours). Set to 0 (zero) to have each cache entry checked every time (note: this will result in a lot more network I/O and negatively impact performance, albeit there is still a substantial performance saving vs not using the cache at all).
43+
1. `org.spdx.storage.listedlicense.enableCache` - a boolean that enables or disables the WebStore's local cache. Defaults to `false` (the cache is disabled). The cache location is determined as per the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) i.e. `${XDG_CACHE_HOME}/Spdx-Java-Library` or `${HOME}/.cache/Spdx-Java-Library`).
44+
2. `org.spdx.storage.listedlicense.cacheCheckIntervalSecs` - a long that controls how often each cache entry is rechecked for staleness, in units of seconds. Defaults to 86,400 seconds (24 hours). Set to 0 (zero) to have each cache entry checked every time (note: this will result in a lot more network I/O and negatively impact performance, albeit there is still a substantial performance saving vs not using the cache at all).
4545

4646
Note that both of these configuration options can only be modified prior to initialization of Spdx-Java-Library. Once the library is initialized, subsequent changes to either or both of these Java properties will have no effect.
4747

src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseWebStore.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public class SpdxListedLicenseWebStore extends SpdxListedLicenseModelStore {
7676
System.getenv("XDG_CACHE_HOME")) +
7777
"/Spdx-Java-Library";
7878

79-
private final boolean cacheEnabled = Boolean.parseBoolean(
80-
System.getProperty("org.spdx.storage.listedlicense.SpdxListedLicenseWebStore.enableCache"));
79+
private final String JAVA_PROPERTY_CACHE_ENABLED = "org.spdx.storage.listedlicense.enableCache";
80+
private final String JAVA_PROPERTY_CACHE_CHECK_INTERVAL_SECS = "org.spdx.storage.listedlicense.cacheCheckIntervalSecs";
81+
private final boolean cacheEnabled;
8182
private final long cacheCheckIntervalSecs;
8283

8384
private final DateTimeFormatter iso8601 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000'Z'").withZone(ZoneOffset.UTC);
@@ -87,18 +88,22 @@ public class SpdxListedLicenseWebStore extends SpdxListedLicenseModelStore {
8788
*/
8889
public SpdxListedLicenseWebStore() throws InvalidSPDXAnalysisException {
8990
super();
90-
if (cacheEnabled) {
91+
92+
// Initialise cache
93+
boolean tmpCacheEnabled = Boolean.parseBoolean(System.getProperty(JAVA_PROPERTY_CACHE_ENABLED));
94+
if (tmpCacheEnabled) {
9195
try {
9296
final File cacheDirectory = new File(cacheDir);
9397
Files.createDirectories(cacheDirectory.toPath());
9498
} catch (IOException ioe) {
95-
throw new InvalidSPDXAnalysisException("Unable to create cache directory: " + cacheDir, ioe);
99+
logger.warn("Unable to create cache directory '" + cacheDir "'; continuing with cache disabled.", ioe);
100+
tmpCacheEnabled = false;
96101
}
97102
}
103+
cacheEnabled = tmpCacheEnabled;
98104
long tmpCacheCheckIntervalSecs = DEFAULT_CACHE_CHECK_INTERVAL_SECS;
99105
try {
100-
tmpCacheCheckIntervalSecs = Long.parseLong(
101-
System.getProperty("org.spdx.storage.listedlicense.SpdxListedLicenseWebStore.cacheCheckIntervalSecs"));
106+
tmpCacheCheckIntervalSecs = Long.parseLong(System.getProperty(JAVA_PROPERTY_CACHE_CHECK_INTERVAL_SECS));
102107
} catch(NumberFormatException nfe) {
103108
// Ignore parse failures - in this case we use the default value of 24 hours
104109
}

0 commit comments

Comments
 (0)