Google guava キャッシュを使用してキャッシュを作成しました
これが私の実装です
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 という 2 つの値を渡す必要があります。
現在、apiKey は一定であるため、正常に動作しています。しかし今、apiKey はこの定数 ~ タイムスタンプに変更されています。例えば:
SOLR_API_KEY~123456789
だから私の問題は:
キーを変更せずにchannelServiceCache.get(key, extraparam here) にもう 1 つのパラメーターを渡すにはどうすればよいですか。
私が試したこと:実際のキーとapiKeyが存在するKeyオブジェクトを作成し、それをchannelServiceCacheのキーとして渡します しかし、タイムスタンプが含まれているため、すべてのキルは新しいキーと見なされるため、これはキャッシュの目的を殺しますアピキーで。
Google Guavaでこれを行う方法はありますか?
編集:私が見逃したもう1つのこと:
サービスは、同じ channelId に対して同じ出力を提供します。API キーは、認証、ロギング、およびリクエスト カウントのモニタリングにのみ使用されます。しかし、それは理にかなっていると思います。キャッシュから同じchannelIDで次のリクエストを処理できる場合、apiKeyは実際のサービス(実際のロギングと監視が行われている場所)に渡されることはありません。グーグルグアバの殺害目的。