C.y in sof
C.y in sof

Reputation: 191

when spring call get(Object o, Class<T> aClass) function when using @Cacheable

I'm using @Cacheable in springboot and implement Cache interface overwriting the following function :

 public String getName()
 public Object getNativeCache()
 public ValueWrapper get(Object key)
 public void put(Object key, Object value)
 public ValueWrapper putIfAbsent(Object key, Object value)
 public void evict(Object key)
 public void clear()

but I leave other two function return null

public <T> T get(Object o, Class<T> aClass)
public <T> T get(Object o, Callable<T> callable)

Now @Cacheable works fine,but when spring will call the two function and is it ok keeping them return null?

Upvotes: 1

Views: 545

Answers (2)

Shameem Khan
Shameem Khan

Reputation: 1

I don't know my answer is correct or not but if you do this way it will work. Create CacheConfig.java

  @Configuration
  @EnableCaching
  public class CacheConfig {

  @Bean
  public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    cacheManager.setCaches(Arrays.asList(new 
    ConcurrentMapCache(Constants.CATEGORYCACHE));
    return cacheManager;
  }

After that you can use this method level Annotation in your ServiceImpl.class

  @Cacheable(value=Constants.CATEGORYCACHE)
  public List<Category> getCategoryList(Category category) {
    return categoryDAO.getCategoryList(category);
  }

 @CacheEvict(value=Constants.CATEGORYCACHE,allEntries=true)
 public void deleteCategory(Category category) {
    categoryDAO.deleteCategory(category);
 }

And see SpringBoot official Doc here : https://howtodoinjava.com/spring-boot2/spring-boot-cache-example/enter link description here

Upvotes: 0

Taylor
Taylor

Reputation: 4087

Yes it's a problem. You've failed to implement the interface. This violates https://en.wikipedia.org/wiki/Liskov_substitution_principle (The "L" in "SOLID").

It presents a trap for subsequent development where consumers of the interface have to know what to avoid on the underlying implementation.

In short, you've failed to meet the contract of the interface.

Upvotes: 1

Related Questions