31, springboot-- cache of JSR107 - @ Caching and @CacheConfig use ⑤

A, @Caching  define complex caching rules

  1, Caching Interface Source:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Caching {
    Cacheable[] cacheable() default {};

    CachePut[] put() default {};

    CacheEvict[] evict() default {};
}

  2, @ Caching use cases: 

    Add a query service method according to lastName employee on the basis of the section

    @Caching(
        cacheable = {
                @Cacheable(value = "emp",key = "#lastName")
        },
        put = {
                @CachePut(value = "emp",key = "#result.id"),
                @CachePut(value = "emp",key = "#result.email"
    =
        the Employee EMPthe Employee getEmpByLastName (String lastName) {public* /
        because @CachePut; CachePut is the result of performing the method into the cache; it must be implementedbut the method according to the employee lastName query will be executed for each query, not taken from the cache;/ *this case would id, email, lastname, respectively, as the data key are loaded after the cache//
    )
        })
    
     employeeMapper.getEmpByLastName(lastName);
        return emp;
    }

    employeeMapper added the appropriate mapping sql

@Select("select * from employee where last_name = #{lastName}")
    public Employee getEmpByLastName(String lastName);

    The method defined in employeeController

@RestController
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @GetMapping("/emp/{id}")
    public Employee getEmployee(@PathVariable("id") Integer id){
        Employee emp = employeeService.getEmp(id);
        return emp;
    }

    @GetMapping("/emp")
    public Employee updateEmployee(Employee employee){
        Employee emp = employeeService.updateEmp(employee);
        return emp;
    }

    @GetMapping("/delEmp")
    public String deleteEmployee(Integer id){
        employeeService.deleteEmp(id);
        return "success";
    }

    @GetMapping("/emp/lastName/{lastName}")
    public Employee getEmpByLastName(@PathVariable("lastName") String lastName){
        Employee emp = employeeService.getEmpByLastName(lastName);
        return emp;
    }
}

    Test Procedure:

      Restart the project, according to the first link in a browser to access lastName query

 

 

   Sql queries sent (At this time the id, email, lastname, respectively, as the data key are loaded after the cache)

  According to Zhang then query the employee id :( Then the console does not issue sql, explained that the query from the cache )

 

   But we then lastName according to Zhang query, or issued a sql query, do not take from the cache; because of the configuration of the @CachePut; CachePut is the result of the implementation of the method into the cache; it must be implemented

 

Two, @ CacheConfig: designate a class cached common configuration, a configuration is not a method

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CacheConfig {
    String[] cacheNames() default {};

    String keyGenerator() default "";

    String cacheManager() default "";

    String cacheResolver() default "";
}

  Configured on the class of:

 

Guess you like

Origin www.cnblogs.com/lyh233/p/12561414.html