Skip to content

Commit

Permalink
Merge pull request #2531 from FarmGeek4Life/master
Browse files Browse the repository at this point in the history
Fixes for #2522
  • Loading branch information
hazendaz authored Sep 22, 2024
2 parents 1dc8c35 + 4b30226 commit 7df2a79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.NoSuchElementException;
import java.util.ServiceLoader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A semi-persistent mapping from keys to values.
*
Expand Down Expand Up @@ -60,13 +63,22 @@ public interface Cache<K, V> {
*/
static <K, V> Cache<K, V> newCache(int timeout) throws NoSuchElementException {
final NoSuchElementException exception = new NoSuchElementException();
boolean cacheSupplierFound = false;
for (CacheSupplier cacheSupplier : ServiceLoader.load(CacheSupplier.class)) {
cacheSupplierFound = true;
try {
return cacheSupplier.newCache(timeout);
} catch (Exception e) {
exception.addSuppressed(e);
}
}

if (!cacheSupplierFound) {
Logger logger = LoggerFactory.getLogger(Cache.class);
logger.error("No CacheSupplier implementation found by ServiceLoader. Please see https://github.com/Waffle/waffle/blob/master/Docs/faq/CustomCache.md for possible solutions. Falling back to default CaffeineCache implementation.", exception);
return new CaffeineCache<>(timeout);
}

throw exception;
}

Expand Down
12 changes: 12 additions & 0 deletions Source/JNA/waffle-jna/src/main/java/waffle/util/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.NoSuchElementException;
import java.util.ServiceLoader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A semi-persistent mapping from keys to values.
*
Expand Down Expand Up @@ -60,13 +63,22 @@ public interface Cache<K, V> {
*/
static <K, V> Cache<K, V> newCache(int timeout) throws NoSuchElementException {
final NoSuchElementException exception = new NoSuchElementException();
boolean cacheSupplierFound = false;
for (CacheSupplier cacheSupplier : ServiceLoader.load(CacheSupplier.class)) {
cacheSupplierFound = true;
try {
return cacheSupplier.newCache(timeout);
} catch (Exception e) {
exception.addSuppressed(e);
}
}

if (!cacheSupplierFound) {
Logger logger = LoggerFactory.getLogger(Cache.class);
logger.error("No CacheSupplier implementation found by ServiceLoader. Please see https://github.com/Waffle/waffle/blob/master/Docs/faq/CustomCache.md for possible solutions. Falling back to default CaffeineCache implementation.", exception);
return new CaffeineCache<>(timeout);
}

throw exception;
}

Expand Down

0 comments on commit 7df2a79

Please sign in to comment.