Grails Redis缓存-间接调用@Cacheable带注释的方法-如何工作

时间:2018-06-19 15:27:25

标签: caching grails redis

我在grails 2.5.3中遇到以下情况:

EmployeeService{

    @Cacheable(value = "employees")
    public List<Employee> getEmployeeList() {
        return Employee.list()
    }

    public Employee getEmployee(long id){
        return getEmployeeList().find{it.id == id}
    }
}



EmployeeController{

    def employeeService

    def getEmployee(long id){
        render employeeService.getEmployee(id) as JSON
    }
}

我的问题是-@Cacheable中带有EmployeeService注释的方法没有将数据存储在Redis缓存中,我该怎么做才能完全实现这种情况。

谢谢。

1 个答案:

答案 0 :(得分:1)

缓存是由spring提供的。因此,您需要使用spring方法来调用方法以应用缓存:

EmployeeService {
    def grailsApplication

    @Cacheable(value = "employees")
    public List<Employee> getEmployeeList() {
        return Employee.list()
    }

    public Employee getEmployee(long id){
        return proxy.getEmployeeList().find {it.id == id}
    }

    // using springs bean proxy ensures cacheable aspects are applied
    private getProxy() {
        grailsApplication.mainContext.employeeService
    }
}