简体   繁体   中英

Where to put @Cacheable annotation

What is the best practice on where to put @Cacheable annotation. Should it be in Interface (Myservice) or in the implementation (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM