我正在使用咖啡因缓存,并且在缓存中大约有7-8万条记录,而刷新时我不想刷新孔缓存,因为它需要10-11分钟才能完成。那么有什么方法可以只刷新更改后的值
我尝试了多种方法,例如Ehcache,番石榴,咖啡因
Caffeine.newBuilder().maximumSize(1000).refreshAfterWrite(11, TimeUnit.MINUTES)
控制器代码
package com.cache.code.CaffeineCache.iHexa.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.cache.code.CaffeineCache.iHexa.beans.CategoryType;
import com.cache.code.CaffeineCache.iHexa.beans.Ctgry;
import com.cache.code.CaffeineCache.iHexa.beans.Factory;
import com.cache.code.CaffeineCache.iHexa.service.CategoryRelationshipService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.ibm.db2.jcc.a.c;
@RestController
@Component
public class CategoryTypeController {
@Autowired
CategoryRelationshipService categoryRelationshipService;
@RequestMapping(value = "/loadRelationShip", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Ctgry> loadRelationShip() {
List<Ctgry> ctgries = null;
try {
ctgries = categoryRelationshipService.loadCategoryRelationship();
} catch (Exception ee) {
System.out.println(ee);
}
return ctgries;
}
@RequestMapping(value = "/loadRelationShipById/{r_id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Ctgry loadRelationShipById(@PathVariable String r_id) {
Ctgry ctgries = null;
List<Ctgry> list = null;
try {
list = categoryRelationshipService.loadCategoryRelationship();
if (list != null) {
ctgries = list.stream().filter(x -> Integer.parseInt(r_id) == x.getCtgry_id()).findAny().orElse(null);
}
} catch (Exception ee) {
System.out.println(ee);
}
return ctgries;
}
}
服务代码
package com.cache.code.CaffeineCache.iHexa.serviceImpl;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.cache.code.CaffeineCache.iHexa.beans.CategoryType;
import com.cache.code.CaffeineCache.iHexa.beans.Ctgry;
import com.cache.code.CaffeineCache.iHexa.beans.Factory;
import com.cache.code.CaffeineCache.iHexa.dao.CategoryRelationshipDao;
import com.cache.code.CaffeineCache.iHexa.service.CategoryRelationshipService;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
@Service
public class CategoryRelationshipServiceImpl implements CategoryRelationshipService {
@Autowired
CategoryRelationshipDao categoryRelationshipDao;
LoadingCache<String, List<Ctgry>> pairsCacheCtgry = null;
@Override
public List<Ctgry> loadCategoryRelationship() {
if (pairsCacheCtgry == null) {
pairsCacheCtgry = Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(11, TimeUnit.MINUTES)
.refreshAfterWrite(11, TimeUnit.MINUTES).build(title -> { // Using a jOOQ repository
System.out.println("!!!!!!!!!!!!!!!!");
return categoryRelationshipDao.loadCategoryRelationship();
});
}
return pairsCacheCtgry.get("");
}
}
所以任何对我有用的建议