如何在 google guava 缓存中多传递一个参数而不是 key

How to pass two pass one more parameter other than key in google guava cache

我已经使用 google 番石榴缓存创建了一个缓存

这是我的实现

private final LoadingCache<Long, DistChannel> channelServiceCache = CacheBuilder.newBuilder().maximumSize(50)
        .refreshAfterWrite(4, TimeUnit.HOURS).build(new CacheLoader<Long, DistChannel>() {

            @Override
            public DistChannel load(Long channelId) throws InvalidRequestException, TException {
                long start = System.nanoTime();
                try {
                    return channelService.getDistributionChannelById(channelId, SOLR_API_KEY);
                } catch (InvalidRequestException e) {
                    log.error("Cannot look up channel: {}", channelId, e);
                    String serviceName = StringUtils.isNotBlank(e.getServiceName()) ? e.getServiceName() : CHANNEL_SERVICE;
                    throw e.setServiceName(serviceName + "." + SERVICE_NAME + "." + hostName);
                } finally {
                    log.info("Channel Service call, ChannelId: {} Time : {}", channelId,
                            TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
                }
            }


            @Override
            public ListenableFuture<DistChannel> reload(final Long channelId, final DistChannel oldValue) throws Exception {
                ListenableFutureTask<DistChannel> task = ListenableFutureTask.create(new Callable<DistChannel>() {
                    public DistChannel call() {
                        long start = System.nanoTime();
                        try {
                            return channelService.getDistributionChannelById(channelId, SOLR_API_KEY);
                        } catch (TException e) {
                            log.error("Cannot look up channel: {}", channelId, e);
                        }finally {
                            log.info("reload Channel Service call, ChannelId: {} Time : {}", channelId,
                                    TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
                        }
                        return oldValue;
                    }
                });
                executorServiceForCache.execute(task);
                return task;
            }

        });

现在在 channelService.getDistributionChannelById 方法中,我需要传递两个值,即 channelId 和 apiKey。

目前它工作正常,因为 apiKey 是恒定的。但是现在apiKey修改为这个contant~timestamp。例如:

SOLR_API_KEY~123456789

所以我的问题是:

How can I pass one more parameter in channelServiceCache.get(key, extraparam here) without modifying the key.

我试过的东西: 我创建了一个 Key 对象,其中将存在我的实际密钥和 apiKey 并将其作为密钥传递给 channelServiceCache 但这将破坏缓存的目的,因为每次杀死都将被视为新的密钥,因为它在 apikey 中包含时间戳。

Is there any way I can do this with google guava?

编辑:我错过的另一件事:

service 将为相同的 channelId 提供相同的输出,API key 仅用于身份验证和日志记录以及监控请求计数。但我想这是有道理的,如果我可以从缓存中使用相同的 channelID 为下一个请求提供服务,那么 apiKey 将永远不会传递给实际服务(实际进行日志记录和监视的地方)。是否有任何其他方法可以在没有实际的情况下实现此功能google番石榴的杀戮目的。

如果每个频道 ID 和 API 键的服务输出不同,则 API 键需要是缓存键的一部分。

如果无论使用什么 API 键输出都是相同的,那么您可以使用任何 API 键或始终相同的 API 键。

从调用者那里传递 API 键,但缓存值没有任何意义,或者本质上等同于随机选择一个 API 键。一旦该值在缓存中,您将 return 一个使用与前一个调用者不同的 API 键检索的结果。