简体   繁体   English

Guava CacheLoader 具有多个键,每个键都有自己的 refreshAfterWrite

[英]Guava CacheLoader with multiple keys each with its own refreshAfterWrite

I currently have a CacheLoader我目前有一个CacheLoader

public class TransactionsDetailsCache extends CacheLoader<String, Transactions> {

    private final Client client;

    private final ExecutorService executor = Executors.newFixedThreadPool(1);

    public TransactionsDetailsCache(Client client) {
        this.client = checkNotNull(client);
    }

    @Override
    public Transactions load(String s) {
        return getTransactionsDetails(client);
    }

    // refreshing here by calling load
    @Override
    public ListenableFuture<Transactions> reload(String key, Transactions oldValue) {
        ListenableFutureTask<Transactions> task = ListenableFutureTask.create(() -> load(key));
        executor.execute(task);
        return task;
    }

    @VisibleForTesting
    protected Transactions getTransactionsDetails(Client Client) {
        Response response = client.getTransactions();

        final Set<TransactionDetails> deniedTransactions = response.stream()
                .filter(transaction -> "denied".equalsIgnoreCase(transaction.getType()))
                .collect(Collectors.toSet());

        final Set<TransactionDetails> allowedTransactions = response.stream()
                .filter(transaction -> "allowed".equalsIgnoreCase(transaction.getType()))
                .collect(Collectors.toSet());

        return new Transactions(allowedTransactions, deniedTransactions);
    }
}

and injecting it using并使用注入它

@Provides
@Singleton
public LoadingCache<String, Transactions> provideTransactionsDetails(Client client) {
    return CacheBuilder.newBuilder()
            .refreshAfterWrite(Duration.ofMinutes(60))
            .build(new TransactionsDetailsCache(client);
}

and my Transactions looks like我的Transactions看起来像

@Data
public class Transactions {
    Set<TransactionDetails> allowedTransactions;
    Set<TransactionDetails> deniedTransactions;
}

I am currently using the following to get Transactions object.我目前正在使用以下内容来获取Transactions object。

transactions = cache.get("transactions");

I want to set different refreshAfterWrite times to allowedTransactions and deniedTransactions and get them using same cache.我想为allowedTransactionsdeniedTransactions设置不同refreshAfterWrite时间,并使用相同的缓存来获取它们。 Is this possible using a single CacheLoader?这可以使用单个 CacheLoader 吗? I see that a key can be passed to reload method but not sure how to use it and refreshAfterWrite different for each key.我看到可以将一个key传递给 reload 方法,但不确定如何使用它,并且每个键的refreshAfterWrite不同。

allowedTransactions = cache.get("allowedTransactions");
deniedTransactions = cache.get("deniedTransactions");

I want cache with key allowedTransactions to reload after 1 hour, and with key deniedTransactions to reload after 5 minutes.我希望使用键allowedTransactions的缓存在 1 小时后重新加载,并使用键deniedTransactions在 5 分钟后重新加载。

I do not want to have different caches because both allowedTransactions and deniedTransactions are received from single API call and want them to be in single cache.我不想拥有不同的缓存,因为allowedTransactionsdeniedTransactions都是从单个 API 调用中接收的,并且希望它们位于单个缓存中。 I feel like this can be achieved with using different key , but not sure how to get expiry for each key, time of last refresh is not stored anywhere.我觉得这可以通过使用不同的key来实现,但不确定如何让每个 key 过期,最后一次刷新的时间不会存储在任何地方。

This feature is not supported by Guava. Guava 不支持此功能。 (I don't believe it's even supported by Caffeine, which is a more advanced replacement for Guava's cache.) (我不相信它甚至得到 Caffeine 的支持,它是 Guava 缓存的更高级替代品。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM