【发布时间】:2020-06-28 09:24:23
【问题描述】:
我正在使用 springboot 和 echCache3 来将一些参数保存在内存中。我是这样配置的:
echcache.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
<cache alias="paramCache">
<listeners>
<listener>
<class>com.telefonica.npro.cacheconf.CacheEventLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache>
</config>
缓存配置
@Configuration
@EnableCaching
public class CacheConfig {
}
CacheEventLogger
public class CacheEventLogger implements CacheEventListener<Object, Object> {
private static final Logger log = LogManager.getLogger(CacheEventLogger.class);
@Override
public void onEvent(CacheEvent<Object, Object> cacheEvent) {
log.info("Cache event {} for item with key {}. Old value = {}, New value = {}", cacheEvent.getType(), cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
}
}
我在 CustomRepositoryImpl 上使用它
public class CustomRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
implements CustomRepository<T, ID> {
....
@Override
@Transactional
@Cacheable(value = "paramCache")
public String getParam(String nombreParametro) {
String valor = (String) entityManager
.createNativeQuery(
"Select valor from npro_configuracion where parametro='" + nombreParametro + "' and activo='S'")
.getSingleResult();
return valor;
}
..
}
但是我没有看到 CacheEventLogger 的任何痕迹,并且每次我要求该值时都在进行查询... 怎么了?
编辑(添加更多信息)
这是我调用 getParam 方法的类。这是@Service 类..
@Service("uorganizativaService")
@Transactional
public class UOrganizativaServiceImpl implements UOrganizativaService {
...
@Override
public List<UnidadOrganizativaDTO> getUOrganizativaFiltered(String id_centro_coste, String nombre_centro_coste,
String id_orden_interna) {
TSpecification<UnidadOrganizativa> uorganizativadSpecification = new TSpecification<UnidadOrganizativa>();
if (nombre_centro_coste != null && !nombre_centro_coste.isEmpty() && !nombre_centro_coste.equals("null")) {
logger.debug("Filtramos por el campo nombre_centro_coste = " + nombre_centro_coste);
uorganizativadSpecification
.add(new SearchCriteria("nombre_centro_coste", nombre_centro_coste, SearchOperation.MATCH_END));
}
if (id_centro_coste != null && !id_centro_coste.isEmpty() && !id_centro_coste.equals("null")) {
logger.debug("Filtramos por el campo id_centro_coste = " + id_centro_coste);
uorganizativadSpecification
.add(new SearchCriteria("id_centro_coste", id_centro_coste, SearchOperation.MATCH_END));
}
if (id_orden_interna != null && !id_orden_interna.isEmpty() && !id_orden_interna.equals("null")) {
logger.debug("Filtramos por el campo id_orden_interna = " + id_orden_interna);
uorganizativadSpecification
.add(new SearchCriteria("id_orden_interna", id_orden_interna, SearchOperation.MATCH_END));
}
uorganizativadSpecification.add(new SearchCriteria("activo", "S", SearchOperation.EQUAL));
Pageable topfifty = new PageRequest(0, 50);
Page<UnidadOrganizativa> listaUorganizativa = uorganizativaRepository.findAll(uorganizativadSpecification,
topfifty);
String totalRegistrosMostrar = uorganizativaRepository.getParam("MAX_REGISTROS_A_MOSTRAR");
//Total de elementos
long totalRegistrosBD = listaUorganizativa.getTotalElements();
List<UnidadOrganizativa> lista = listaUorganizativa.getContent();
// Indicador para advertir si mostramos todos los registros o no
boolean registrosLimitados = totalRegistrosBD > Integer.valueOf(totalRegistrosMostrar) ? true : false;
List<UnidadOrganizativaDTO> listaUOrganizativaDTO = lista.stream().map(e -> new UnidadOrganizativaDTO(e,registrosLimitados))
.collect(Collectors.toList());
return listaUOrganizativaDTO;
}
问候
【问题讨论】:
-
首先我认为你应该把它引入你的 application.properties 文件 spring.cache.jcache.config=classpath:ehcache.xml 其次如果你使用缓存监听器来调试和记录你的缓存操作不需要它们。您可以启用跟踪以查看缓存操作与这个 logging.level.org.springframework.cache=TRACE
-
您好,已经添加。问题是在存储库上使用@cacheable,我用其他方法进行了测试,但在服务中,它正在运行。在存储库上使用它有什么问题吗?我认为这种方式很有用,而不是仅为参数创建服务和模型
-
您能否分享一下您是如何从哪个类调用此存储库方法的?
-
编辑添加信息,如您所见,我是从服务类调用的。 UOrganizativaRepository 扩展自 CustomRepository 和 JPARepository
-
我没有看到对“getParametro”方法的任何调用
标签: java spring-boot hibernate ehcache-3