> + final Object fakeKey = new Object(); // fake key for single-element
> cache
> + final LoadingCache<Object, Authorization> cache =
> CacheBuilder.newBuilder()
> + .expireAfterWrite(23, TimeUnit.HOURS)
> + .build(CacheLoader.from(new Function<Object, Authorization>() {
> + @Override
> + public Authorization apply(Object nothing) {
> + return b2Api.getAuthorizationApi().authorizeAccount();
> + }
> + }));
> + return new Supplier<Authorization>() {
> + @Override
> + public Authorization get() {
> + return cache.getUnchecked(fakeKey);
> + }
> + };
> + }
A common practice in jclouds when you are providing a supplier on top of a
cache and binding it to the Guice contex, is to use the jclouds memoized
supplier. It already takes care of properly dealing with auth exceptions. It
would be something like:
```java
@Provides
@Singleton
protected final Supplier<Authorization> provideAuthorizationSupplier(final
B2Api b2Api) {
return new Supplier<Authorization>() {
@Override public Authorization get() {
return b2Api.getAuthorizationApi().authorizeAccount();
}
};
}
@Provides
@Singleton
@Memoized
protected final Supplier<Authorization> provideAuthorizationCache(
AtomicReference<AuthorizationException> authException,
@Named(PROPERTY_SESSION_INTERVAL) long seconds,
Supplier<Authorization> uncached) {
return MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.create(
authException, uncached, seconds, TimeUnit.SECONDS);
```
With this you can inject both suppliers. If you want to inject the cache, you
just need to qualify the variable as `@Memoized`.
Also note that the `@Provides` methods are better marked final, as Guice 4 does
not allow to override them. I've also used the `PROPERTY_SESSION_INTERVAL`
property to configure the auth expiration. Feel free to consider if that makes
sense in this provider. If it does, you could configure the 23 hours default
value for that property in the api metadata.
---
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-labs/pull/270/files/2a136008e73694853e887a138883638321393bdd#r64875953