我在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缓存中,我该怎么做才能完全实现这种情况。
谢谢。
答案 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
}
}