[英]Where to put @Cacheable annotation
What is the best practice on where to put @Cacheable annotation.关于在哪里放置 @Cacheable 注释的最佳实践是什么。 Should it be in Interface (Myservice) or in the implementation (MyserviceImpl) class.
它应该在接口(Myservice)中还是在实现(MyserviceImpl)中 class。
public class MyClass
{
public final Map<String, String> getDepartmentsList(String userid) {
System.out.println("I am getting the dept" );
Map<String, String> deptMap = myService.getList(userid);
return deptMap;
}
}
@Service
public class MyserviceImpl implements Myservice
{
****@Cacheable(cacheName = "myList", keyGeneratorName = "cacheKeyGenerator" )****
public Map<String, String> getList(String userid){
}
}
public interface Myservice {
**@Cacheable(cacheName = "myList", keyGeneratorName = "cacheKeyGenerator" )**
public Map<String, String> getList(String userid);
}
There is no silver bullet.没有灵丹妙药。 It depends on your purposes.
这取决于你的目的。 If you declare it on the interface you declare: all implementations have a cached method.
如果你在你声明的接口上声明它:所有的实现都有一个缓存的方法。 But probably in most cases, you can hide these details from your API/SPI user in a specific implementation.
但可能在大多数情况下,您可以在特定的实现中向您的 API/SPI 用户隐藏这些细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.