package com.lithium.dbi.rdbi.recipes.cache;

import com.lithium.dbi.rdbi.RDBI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;

public class AsyncCacheRefresher<KeyType, ValueType> implements Callable<CallbackResult<ValueType>> {
    private static final Logger log = LoggerFactory.getLogger(RDBI.class);
    private final RedisCache<KeyType, ValueType> cache;
    private final KeyType key;
    private final RDBI rdbi;

    protected AsyncCacheRefresher(final RedisCache<KeyType, ValueType> cache,
                                  final RDBI rdbi,
                                  final KeyType key) {
        this.cache = cache;
        this.rdbi = rdbi;
        this.key = key;
    }

    @Override
    public CallbackResult<ValueType> call() {
        final long start = System.currentTimeMillis();

        if(!cache.acquireLock(key)) {
            log.debug("{}: Unable to acquire refresh lock for", cache.getCacheName());
            cache.markLoadException(System.currentTimeMillis() - start);
            return new CallbackResult<>(new LockUnavailableException());
        }

        log.debug("{}: Attempting to refresh data for", cache.getCacheName());

        try {
            final ValueType value = cache.load(key);
            if (value == null) {
                cache.markLoadException(System.currentTimeMillis() - start);
                return new CallbackResult<>();
            }

            log.info("{}: Async refresh for {}", cache.getCacheName());
            cache.put(key, value); // this shouldn't throw, the withHandle eats it...
            cache.markLoadSuccess(System.currentTimeMillis() - start);
            return new CallbackResult<>(value);
        } catch (Exception ex) {
            cache.markLoadException(System.currentTimeMillis() - start);
            return new CallbackResult<>(ex);
        } finally {
            cache.releaseLock(key);
        }
    }
}

@@ -943,24 +943,27 @@
 esh lock for
+ %7B%7D
 %22, cache.get
@@ -965,32 +965,37 @@
 e.getCacheName()
+, key
 );%0A            c
@@ -1191,16 +1191,19 @@
 data for
+ %7B%7D
 %22, cache
@@ -1217,16 +1217,21 @@
 heName()
+, key
 );%0A%0A    
@@ -1534,16 +1534,21 @@
 heName()
+, key
 );%0A     

Fix log messages like this: EngagementConfigCache: Async refresh for {}