java - 用java加载缓存中的数据

标签 java hibernate browser-cache ehcache

我对一个问题有点迷茫。我需要在缓存中保存从数据库查询的数据列表。该列表非常大,当它加载到组合框中时并不是最佳选择。对此有什么想法吗?

我的代码是:

  • 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,这将表明调用一个方法(或类中的所有方法)的结果可以被缓存。

每次调用建议的方法时,都会应用缓存行为,检查是否已经为给定的参数调用了该方法:

Here's an example

关于java - 用java加载缓存中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53225186/

相关文章:

java - Eclipse - 尝试基于 WSDL 文件创建 Web 服务时出现意外异常

java - 如何找到最大值和最小值

java - 我如何指示 IntelliJ Idea 不包含某些依赖项(android)

java - Google Vision Java 客户端批量请求响应识别

hibernate - 在 JSF 中使用 JPA 实体。防止 LazyInitializationException 的最佳策略是什么?

spring - 枚举值不保留为 EnumType.STRING,而是保留为子实体的 ORDINALvalue

html - 资源在 Google Chrome 网络选项卡中显示待处理和来自缓存

java - 如何在 hibernate 中忽略加载中的对象引用重用?

javascript - 禁用缓存后,Chrome CORS 请求会更快吗?

google-chrome - 如何清除 Google Chrome iPad 应用程序上的应用程序缓存?