gpt4 book ai didi

spring-cache - Spring缓存动态设置过期时间 - Caffeine

转载 作者:行者123 更新时间:2023-12-05 01:41:52 71 4
gpt4 key购买 nike

我使用身份验证 API 获取 token 并使用其他服务。此 API 返回 token 和过期时间。有可能获得它返回的过期时间并用这些值设置 expire_after_write 吗?目前此属性在 application.properties 中,但我担心有一天服务提供商会更改 expire_time 并且我们会收到一些错误,因为 token 已过期

最佳答案

你可以设置一个per-entry policy评估条目并确定到期时间。由于 token 不会在缓存中修改,您可以使用 expireAfterCreate 并让其他方法返回 currentDuration 以不修改它。从文档中,

LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
.expireAfter(new Expiry<Key, Graph>() {
public long expireAfterCreate(Key key, Graph graph, long currentTime) {
// Use wall clock time, rather than nanotime, if from an external resource
long seconds = graph.creationDate().plusHours(5)
.minus(System.currentTimeMillis(), MILLIS)
.toEpochSecond();
return TimeUnit.SECONDS.toNanos(seconds);
}
public long expireAfterUpdate(Key key, Graph graph,
long currentTime, long currentDuration) {
return currentDuration;
}
public long expireAfterRead(Key key, Graph graph,
long currentTime, long currentDuration) {
return currentDuration;
}
})
.build(key -> createExpensiveGraph(key));

关于spring-cache - Spring缓存动态设置过期时间 - Caffeine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53089849/

71 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com