Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多级缓存下,仅通过环境变量配置本地过期时间无法生效 #915

Open
Roiocam opened this issue Aug 22, 2024 · 8 comments · May be fixed by #916
Open

多级缓存下,仅通过环境变量配置本地过期时间无法生效 #915

Roiocam opened this issue Aug 22, 2024 · 8 comments · May be fixed by #916

Comments

@Roiocam
Copy link
Contributor

Roiocam commented Aug 22, 2024

背景

多级缓存下(cacheType = BOTH) 如果不配置注解的 localExpire ,那么通过配置文件指定的本地过期时间不会生效

jetcache:
  local:
    default:
      type: caffeine
      limit: 100
      # 不会生效
      expireAfterWriteInMillis: 100000

核心代码:

Config 只会从注解里拿

boolean useExpireOfSubCache = config.getLocalExpire() != null;

@areyouok
Copy link
Collaborator

buildLocal的时候会生效的

@Roiocam
Copy link
Contributor Author

Roiocam commented Aug 22, 2024

buildLocal的时候会生效的

LocalCache 这个对象里面的值会生效,但是 MultiLevelCache 的逻辑不会,取的是 MultiLevelCache 里面的过期时间

if (config.isUseExpireOfSubCache()) {
return PUT(key, value, 0, null);
} else {
return PUT(key, value, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS);

而这个 MultiLevelCache 过期时间的取值逻辑是最后一个 Cache 的过期时间(也就是远程过期时间):

CacheConfig lastConfig = caches[caches.length - 1].config();
config = new MultiLevelCacheConfig<>();
config.setCaches(Arrays.asList(caches));
config.setExpireAfterWriteInMillis(lastConfig.getExpireAfterWriteInMillis());

cache = MultiLevelCacheBuilder.createMultiLevelCacheBuilder()
.expireAfterWrite(remote.config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS)
.addCache(local, remote)
.useExpireOfSubCache(useExpireOfSubCache)

@areyouok
Copy link
Collaborator

我明白你的意思了,你的意思是useExpireOfSubCache是多余的。

但当时就这么设计的,现在不确定是否应该改。

@Roiocam
Copy link
Contributor Author

Roiocam commented Aug 22, 2024

我明白你的意思了,你的意思是useExpireOfSubCache是多余的。

但当时就这么设计的,现在不确定是否应该改。

嗯,有点像是,如果 LocalCacheRemoteCache 各自能通过环境变量覆盖,那么 MultiLevelCache 应该不需要处理这一部分逻辑。现在的代码导致做不到:

环境变量 -> 应用配置 -> 硬编码

这种优先级了

@areyouok
Copy link
Collaborator

localExpire是18年后来加的,原先有两层的话,就以remote的为准

Roiocam added a commit to Roiocam/jetcache that referenced this issue Aug 22, 2024
@Roiocam Roiocam linked a pull request Aug 22, 2024 that will close this issue
2 tasks
@Roiocam
Copy link
Contributor Author

Roiocam commented Aug 22, 2024

localExpire是18年后来加的,原先有两层的话,就以remote的为准

我提了个 PR #916 请您看一下?去掉了 useExpireOfSubCache:

  • 总是让每层 Caches 选择自己参数的过期时间,除非是用户自己指定的覆盖
  • 对于 GET 本地过期,去拿远程后,会用远程的过期时间更新本地,但又尊重本地缓存的过期时间

@areyouok
Copy link
Collaborator

我还没细看,但至少有两个问题:

  1. 需要保证兼容性。已经公开的api是不能删除的,不用了可以标注为废弃。
  2. 不要有中文注释。

@Roiocam
Copy link
Contributor Author

Roiocam commented Aug 22, 2024

我还没细看,但至少有两个问题:

  1. 需要保证兼容性。已经公开的api是不能删除的,不用了可以标注为废弃。
  2. 不要有中文注释。

ok, 修复了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants