-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating a Caffeine LoadingCache using a Guava CacheLoader #766
Comments
I suppose it depends on which features of Guava's CacheLoader you are implementing. I think you could write an adapter without too much trouble. Single loadingThis is a 1-to-1 so you can use a method reference LoadingCache<K, V> cache = Caffeine.newBuilder().build(guavaCacheLoader::load); Bulk loadingThis is slightly harder since Caffeine detects if LoadingCache<K, V> cache = Caffeine.newBuilder().build(new CacheLoader<K, V>() {
public K load(K key) {
return guavaCacheLoader.load(key);
}
public Map<K, V> loadAll(Set<? extends K> keys) {
return guavaCacheLoader.loadAll(keys);
}
}); If you don't mind using the bulk LoadingCache<K, V> cache = Caffeine.newBuilder().build(CacheLoader.bulk(guavaCacheLoader::loadAll)); ReloadingIf you customize the import net.javacrumbs.futureconverter.java8guava.FutureConverter;
LoadingCache<K, V> cache = Caffeine.newBuilder().build(new CacheLoader<K, V>() {
public K load(K key) {
return guavaCacheLoader.load(key);
}
public CompletableFuture<? extends V> asyncReload(K key, V oldValue, Executor executor) {
return FutureConverter.toCompletableFuture(guavaCacheLoader.reload(key, oldValue));
}
}); |
The Guava adapters wrap the Caffeine implementations to masquerade under their APIs. Sometimes users wish to use Caffeine's APIs without migrating their Guava CacheLoader. The adapter is now available for use with our cache builder. ```java CacheLoader<K, V> caffeineLoader = CaffeinatedGuava.caffeinate(guavaLoader); LoadingCache<K, V> caffeineCache = Caffeine.newBuilder().build(caffeineLoader); ```
I've exposed our existing adapters in the Guava package. I'm not sure when the next release will be, so you might review that code to embed something similar locally. It's a little more complex since it handles single, bulk, async loading, and some internal adaptation details. CacheLoader<K, V> caffeineLoader = CaffeinatedGuava.caffeinate(guavaLoader);
LoadingCache<K, V> caffeineCache = Caffeine.newBuilder().build(caffeineLoader); |
The Guava adapters wrap the Caffeine implementations to masquerade under their APIs. Sometimes users wish to use Caffeine's APIs without migrating their Guava CacheLoader. The adapter is now available for use with our cache builder. ```java CacheLoader<K, V> caffeineLoader = CaffeinatedGuava.caffeinate(guavaLoader); LoadingCache<K, V> caffeineCache = Caffeine.newBuilder().build(caffeineLoader); ```
The Guava adapters wrap the Caffeine implementations to masquerade under their APIs. Sometimes users wish to use Caffeine's APIs without migrating their Guava CacheLoader. The adapter is now available for use with our cache builder. ```java CacheLoader<K, V> caffeineLoader = CaffeinatedGuava.caffeinate(guavaLoader); LoadingCache<K, V> caffeineCache = Caffeine.newBuilder().build(caffeineLoader); ```
The Guava adapters wrap the Caffeine implementations to masquerade under their APIs. Sometimes users wish to use Caffeine's APIs without migrating their Guava CacheLoader. The adapter is now available for use with our cache builder. ```java CacheLoader<K, V> caffeineLoader = CaffeinatedGuava.caffeinate(guavaLoader); LoadingCache<K, V> caffeineCache = Caffeine.newBuilder().build(caffeineLoader); ```
The Guava adapters wrap the Caffeine implementations to masquerade under their APIs. Sometimes users wish to use Caffeine's APIs without migrating their Guava CacheLoader. The adapter is now available for use with our cache builder. ```java CacheLoader<K, V> caffeineLoader = CaffeinatedGuava.caffeinate(guavaLoader); LoadingCache<K, V> caffeineCache = Caffeine.newBuilder().build(caffeineLoader); ```
Thanks Ben. That's exactly what we needed. |
We're facing exactly the same situation as @Zuplyx. @ben-manes This issue has been fixed more than two months ago. Any chance to get a release out soon? Thanks and keep up the great work! |
Sorry, I haven't had any time this week to think about it. I was hoping Solr would make progress on their broken key equality bug to confirm it was user error, but that seems to have stalled out. I'll try to revisit this over the weekend, but the code simple and easy to copy for local use. |
Released in 3.1.2 |
Hi,
we are currently in the process of migrating from Guava cache to Caffeine. In our codebase we have several classes implementing Guava's CacheLoader. Ideally we'd like to leave these classes unchanged.
Is there a way to create a Caffeine LoadingCache using a Guava CacheLoader?
Maybe an analogue to CaffeinatedGuava's build method, but which directly returns the created Caffeine cache instead of wrapping it in a Guava cache?
The text was updated successfully, but these errors were encountered: