[英]How to properly cache data in spring boot application with Thymleaf
I am writing some Java code for my Spring boot application to receive a list of 40,000 elements and, through Thymeleaf post it in a dropdown field.我正在为 Spring boot 应用程序编写一些 Java 代码,以接收包含 40,000 个元素的列表,并通过 Thymeleaf 将其发布到下拉字段中。 However, I noticed that when I load the data and go to the dropdown, everything is slow.
但是,我注意到当我加载数据并转到下拉列表时,一切都很慢。
I was told to cache the values and store them.我被告知要缓存这些值并存储它们。 Though I am not too familiar with this process I tried using the
@Cacheable
annotation for my SpringBootApplication, but it still didn't work.虽然我对这个过程不太熟悉,但我尝试对 SpringBootApplication 使用
@Cacheable
注释,但它仍然不起作用。 I am trying to see what I did wrong or if there is a better way to approach this issue I am having.我想看看我做错了什么,或者是否有更好的方法来解决我遇到的这个问题。
Service Layer:服务层:
@SuppressWarnings("unchecked")
@Cacheable("String")
public List<String> getServerListing(){
StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("GetAllServers");
return storedProcedure.getResultList();
}
First, I have a stored procedure bring down all the data from the server, (~40,000 records).首先,我有一个存储过程从服务器中取出所有数据(约 40,000 条记录)。
Controller:控制器:
List<String> servers = joinQueryService.getServerListing();
modelAndView.addObject("servers", servers);
Then, I set it to a List of strings and send it to front-end.然后,我将它设置为一个字符串列表并将其发送到前端。
Thymeleaf:百里香:
<div class="row">
<div class="col col-lg-9 search-bar">
<div class="form-group">
<label>Server:</label> <select class="js-example-basic-single3"
th:field="*{servers}" id="selectData3">
<option value=""></option>
<option th:each="servers : ${servers}" th:value="${servers}" th:text="${servers}" />
</select>
</div>
</div>
</div>
SpringBoot:弹簧靴:
@SpringBootApplication
@Cacheable
public class TaddmDevApplication {
public static void main(String[] args) {
Policy.setPolicy(new TaddmPolicy());
SpringApplication.run(TaddmDevApplication.class, args);
}
}
I think the problem here is: A browser will take several seconds to load and/or display such a large drop-down, regardless of how streamlined the rest of your application is.我认为这里的问题是:浏览器需要几秒钟才能加载和/或显示如此大的下拉菜单,无论您的应用程序的其余部分有多精简。
As a test, I created a text file containing nothing except a 40,000-item drop-down (test.htm):作为测试,我创建了一个文本文件,除了一个包含 40,000 项的下拉列表 (test.htm) 外,什么都不包含:
<!DOCTYPE html>
<html>
<body>
<h2>Create a drop-down List</h2>
<label for="widgets">Choose a widget:</label>
<select id="widgets">
<option value="widget1">Widget 1</option>
<option value="widget2">Widget 2</option>
<option value="widget3">Widget 3</option>
<option value="widget4">Widget 4</option>
<option value="widget5">Widget 5</option>
<option value="widget6">Widget 6</option>
<option value="widget7">Widget 7</option>
... snipped for brevity!
<option value="widget39998">Widget 39998</option>
<option value="widget39999">Widget 39999</option>
<option value="widget40000">Widget 40000</option>
</select>
</body>
</html>
I then opened that file in Chrome and recorded the processing time:然后我在 Chrome 中打开该文件并记录处理时间:
It took about 5 seconds.大约需要 5 秒钟。 There was no database fetch;
没有数据库获取; no network traffic;
没有网络流量; no server-side processing.
没有服务器端处理。
I think the bottom line is: A drop-down of this size will be unwieldy for users, as well as being slow to handle in the browser .我认为底线是:这种大小的下拉菜单对用户来说是笨拙的,并且在浏览器中处理速度很慢。
This means your question becomes a different one: How to send a more modest amount of data to the browser, and allow the user to navigate through the results of each load, one page at a time.这意味着您的问题变得不同:如何向浏览器发送更少量的数据,并允许用户浏览每次加载的结果,一次一页。 I completely agree with your goal of "milliseconds", rather than seconds, by the way.
顺便说一下,我完全同意你的“毫秒”目标,而不是秒。 That is exactly right.
这是完全正确的。 Maybe you already have one, but try to have a specific goal in mind ("under 300 ms" or some such).
也许你已经有了一个,但试着有一个特定的目标(“低于 300 毫秒”或类似的目标)。
Of course, there may well be optimizations in your code - but they are probably moot at this point.当然,您的代码中可能会进行优化 - 但此时它们可能没有实际意义。
Probably not the answer you wanted, but I hope this helped!可能不是您想要的答案,但我希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.