我对一个问题有点迷茫。我需要在缓存中保存从数据库查询的数据列表。该列表非常大,当它加载到组合框中时并不是最佳选择。对此有什么想法吗?
我的代码是:
Controller :
model.addAttribute(LISTA_LUGARES,lugarService.findLugaresByIdMunicipio(documentacionDTO.getIdMunicipio()));
服务:
public List<Lugar> findLugaresByIdMunicipio(Long idMunicipio) { return this.lugarRepository.findLugaresByIdMunicipio(idMunicipio); }
存储库:
@Query("from Lugar l where l.municipio.id = :idMunicipio order by l.nombre asc") List<Lugar> findLugaresByIdMunicipio(@Param("idMunicipio") Long idMunicipio);
HTML:
<select id="lugar" th:field="*{idLugar}" class="form-control"> <option th:each="type : ${lugar}" th:value="${type.id}" th:text="#{${type.nombre}}">opciones</option> </select>
最佳答案
你可以使用EhCache之类的东西将查询设置为@Cacheable
,这将表明调用一个方法(或类中的所有方法)的结果可以被缓存。
每次调用建议的方法时,都会应用缓存行为,检查是否已经为给定的参数调用了该方法:
关于java - 用java加载缓存中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53225186/