ehcache和spring的正确注释
我有一个这样的模型对象-ehcache和spring的正确注释,spring,caching,ehcache,Spring,Caching,Ehcache,我有一个这样的模型对象- class ProductDTO { int id; String code; String description; //getters and setters go here } class ProductServiceImpl implements ProductService { List<ProductDTO> getAllProducts() { //Query to data la
class ProductDTO {
int id;
String code;
String description;
//getters and setters go here
}
class ProductServiceImpl implements ProductService {
List<ProductDTO> getAllProducts() {
//Query to data layer getting all products
}
String getProductDescriptionById(int id) {
//Query to data layer getting product by id
}
@Cacheable("prodCache")
String getProductDescriptionByCode(String code) {
//Query to data layer getting product by code
}
}
我试图在web服务中使用Spring4和ehcache,代码如下-
class ProductDTO {
int id;
String code;
String description;
//getters and setters go here
}
class ProductServiceImpl implements ProductService {
List<ProductDTO> getAllProducts() {
//Query to data layer getting all products
}
String getProductDescriptionById(int id) {
//Query to data layer getting product by id
}
@Cacheable("prodCache")
String getProductDescriptionByCode(String code) {
//Query to data layer getting product by code
}
}
类ProductServiceImpl实现ProductService{
列出getAllProducts(){
//查询到获取所有产品的数据层
}
字符串getProductDescriptionById(int id){
//查询到按id获取产品的数据层
}
@可缓存(“prodCache”)
字符串getProductDescriptionByCode(字符串代码){
//查询到按代码获取产品的数据层
}
}
缓存在具有可缓存注释的getProductDescriptionByCode()方法上运行良好。每次调用getProductDescriptionById()或getProductDescriptionByCode()时,如果缓存未命中,我希望获取所有产品(可能使用getAllProducts(),但不一定)并缓存它们,以便下次检索任何产品。我应该对注释或代码进行哪些添加或更改?因此,当您使用getAllProducts()检索所有产品时,需要对每个产品进行迭代,并使用@CachePut将它们放入缓存中。 您需要将cacheput函数分离一次,以按代码放置描述,按id放置其他描述
List<ProductDTO> getAllProducts() {
List<ProductDTO> productList //get the list from database
for(ProductDTO product : productList) {
putProductDescriptionInCache(product.getDescription(), product.getCode());
}
}
@CachePut(value = "prodCache", key = "#Code")
String putProductDescriptionInCache(String description, String Code){
return description;
}
列出getAllProducts(){
List productList//从数据库获取列表
for(ProductDTO product:productList){
putProductDescriptionInCache(product.getDescription(),product.getCode());
}
}
@CachePut(value=“prodCache”,key=“#Code”)
字符串putProductDescriptionInCache(字符串描述,字符串代码){
返回说明;
}
因此,当您使用getAllProducts()检索所有产品时,需要对每个产品进行迭代,并使用@CachePut将它们放入缓存中。
您需要将cacheput函数分离一次,以按代码放置描述,按id放置其他描述
List<ProductDTO> getAllProducts() {
List<ProductDTO> productList //get the list from database
for(ProductDTO product : productList) {
putProductDescriptionInCache(product.getDescription(), product.getCode());
}
}
@CachePut(value = "prodCache", key = "#Code")
String putProductDescriptionInCache(String description, String Code){
return description;
}
列出getAllProducts(){
List productList//从数据库获取列表
for(ProductDTO product:productList){
putProductDescriptionInCache(product.getDescription(),product.getCode());
}
}
@CachePut(value=“prodCache”,key=“#Code”)
字符串putProductDescriptionInCache(字符串描述,字符串代码){
返回说明;
}
非常感谢,Shubham。我添加了两个方法,其中@CachePut-1表示代码,另一个表示id。我从getAllProducts()为每个产品调用它们。现在,getProductDescriptionInCache()调用getAllProducts()-在返回的列表中找到正确的描述,返回并缓存它。如果我使用相同的代码再次调用getProductDescriptionInCache(),它将正确地从缓存中获取值,但如果我传递不同的代码,它将再次调用getAllProducts(),即使我正在缓存getAllProducts()找到的所有产品。我遗漏了什么。你能粘贴你的getProductDescriptionInCache()函数吗?非常感谢,Shubham。我添加了两个方法,其中@CachePut-1表示代码,另一个表示id。我从getAllProducts()为每个产品调用它们。现在,getProductDescriptionInCache()调用getAllProducts()-在返回的列表中找到正确的描述,返回并缓存它。如果我使用相同的代码再次调用getProductDescriptionInCache(),它将正确地从缓存中获取值,但如果我传递不同的代码,它将再次调用getAllProducts(),即使我正在缓存getAllProducts()找到的所有产品。我遗漏了什么。你能粘贴你的getProductDescriptionInCache()函数吗?