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

@Cached注解使用key SpEL表达式,注解作用在接口上时失效。 #876

Open
thetcc opened this issue Apr 8, 2024 · 9 comments

Comments

@thetcc
Copy link

thetcc commented Apr 8, 2024

说下环境:macOS , JDK 1.8.0_202
jetcache 版本:2.6.7

@Cached(name = CacheConstant.USER_BASE_INFO_KEY_PREFIX, key = "#userId", cacheType = CacheType.BOTH, localExpire = 30, expire = 300, localLimit = 200) UserBaseBO getUserBaseByUserId(Long userId);
如上,如果作用在接口方法上,这样缓存作用失效,每次都会进入到方法体内。感觉是存在bug。
但是如果去掉自定义spEL表达式,是成功的。或者将上述注解,放到实现类中,缓存也是有效的。
即:
//作用在接口方法上,有效。 @Cached(cacheType = CacheType.BOTH, localExpire = 30, expire = 300, localLimit = 200) UserBaseBO getUserBaseByUserId(Long userId);

//作用在实现类方法上,也是有效。 @Cached(name = CacheConstant.USER_BASE_INFO_KEY_PREFIX, key = "#userId", cacheType = CacheType.BOTH, localExpire = 30, expire = 300, localLimit = 200) public UserBaseBO getUserBaseByUserId(Long userId){ //some code }

是什么原因呢

@areyouok
Copy link
Collaborator

areyouok commented Apr 8, 2024

我猜你的接口做javac编译的时候没有指定-parameters参数

@thetcc
Copy link
Author

thetcc commented Apr 9, 2024

我猜你的接口做javac编译的时候没有指定-parameters参数

被你说中了,是没加。但@cached作用在实现类上是可以的,是不是作用在实现类上就不用加-parameters参数了。

@areyouok
Copy link
Collaborator

areyouok commented Apr 9, 2024

注解在接口上或者在类上都可以,但都需要-parameters参数,否则你编译出来的class文件里面没有参数名称,就无法工作了。

@thetcc
Copy link
Author

thetcc commented Apr 9, 2024

注解在接口上或者在类上都可以,但都需要-parameters参数,否则你编译出来的class文件里面没有参数名称,就无法工作了。

我没加-parameters 时,如果注解作用在实现类上,缓存是正常工作的。只是加在接口上时无法生效。

@areyouok
Copy link
Collaborator

areyouok commented Apr 9, 2024

你的接口和实现类编译设置不一样吧,或者行参名称也可以不一样

@thetcc
Copy link
Author

thetcc commented Apr 9, 2024

你的接口和实现类编译设置不一样吧,或者行参名称也可以不一样

一样的,我只是把注解挪了一个地方。其他没有任何改动。参数名称也是一样。

@areyouok
Copy link
Collaborator

areyouok commented Apr 9, 2024

那你得给出一个可以重现的案例了,要完整的,可运行的

@thetcc
Copy link
Author

thetcc commented Apr 10, 2024

那你得给出一个可以重现的案例了,要完整的,可运行的

就是上面开头的例子,我在本地idea中运行,只要把注解从接口换到实现类,就能走缓存,命中缓存。

@areyouok
Copy link
Collaborator

那好吧,我认为没有问题。这个讨论就此结束了。

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

No branches or pull requests

2 participants