1
votes

I am learning spring boot caching to apply this concept in our organization's project and I made a sample project called employe cache. I have four methods in my controller and service component insert, update, get, and getAll.For insert and get @Cacheable is working perfectly. Now I am calling getAllEmployee() first time then it is fetching data from the database. After that I am trying to update with @CachePut it updates the value in the database and again I am calling getAllEmployee() then it didn't return updated value from the cache. I also refer to the documentation for @CachePut. I also refer to some other documents like this and this but I didn't solve my problem. Also, When I am calling, no error is raised.
What I Tried is
These are my two APIs from EmplyeeController.java

@PostMapping(value = "/updateSalary")
private Boolean updateSalary(@RequestParam int salary, @RequestParam Integer id) {
    return empService.updateSalary(salary, id);
}

@GetMapping(value = "/getAllEmployee")
private Object getAllEmployee() {
    List<EmployeeMapping> empList = empService.getAllEmployee();
    return !empList.isEmpty() ? empList : "Something went wrong";
}

These are my two methods from EmployeeService.java. I applied different keys to update the method but didn't work. My getAll() method has no parameter so I tried all the keys techniques for no parameter methods from here then also I didn't get any results.

@CachePut(key = "#root.method.name")
public Boolean updateSalary(int salary, int id) {
    System.err.println("updateSalary method is calling in service");
    if (empRepo.salary(salary, id) != 0) {
        return true;
    }
    return false;
}

@Cacheable(key = "#root.method.name")
public List<EmployeeMapping> getAllEmployee() {
    return empRepo.findAllEmployee();
}

These are my two methods from EmployeeRepository.java. I used @SqlResultSetMappings and @NamedNativeQueriesin EmployeeMetaModel.java with EmployeeMapping.java but there is no error in native query in EmployeeMetaModel.java because it's giving result from database.

@Transactional
@Modifying
@Query("update employee_cache e set e.salary = ?1 where e.id = ?2")
int salary(int salary, int id);

@Query(name = "EmployeeListQuery", nativeQuery = true)
List<EmployeeMapping> findAllEmployee();

Kindly help me to get rid of this I just need an updated value from the cache using getAllEmployee() after updateSalary() called.

1

1 Answers

0
votes

There is an issue with how you've defined caching via annotations. Your @CachePut and @Cacheable don't use the same cache key. What you should actually have is something like this:

@CachePut(value = "employees", key = "T(org.springframework.cache.interceptor.SimpleKey).EMPTY")
public List<EmployeeMapping> updateSalary(int salary, int id) {
    // update salary and return the list of employees
}

@Cacheable(value = "employees")
public List<EmployeeMapping> getAllEmployee() {
    // return the list of employees
}

Here @CachePutand @Cacheable have the same cache key.d Now, when you call the updateSalary() method, @CachePut will replace the existing cached value for key "employees", with the result of the method i.e. list of employees with updated salary.