gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:12:58 25 4
gpt4 key购买 nike

我在 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
}
}

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

提前谢谢你。

最佳答案

缓存由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
}
}

关于caching - Grails Redis 缓存 - 间接调用 @Cacheable 注释方法 - 如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com