Spring MVC - 从二级缓存中加载参考数据 [英] Spring MVC - Loading reference data from second level cache

查看:148
本文介绍了Spring MVC - 从二级缓存中加载参考数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从之前提出的问题继续(链接如下)

I am continuing from earlier question i raised (link below)

Spring MVC - 从服务器启动时的数据库获取参考数据

在早期发布了一些建议之后,我认为我可以用来加载参考数据的方法是在ArticleController(我的控制器类)中添加下面的方法

After getting some advice on earlier post, the approach i think I can use to load reference data is, add below method in ArticleController (my controller class)

    @ModelAttribute
    public void populateModel(@RequestParam String number, Model model) {
        model.addAttribute("countryList", articleService.getCountryList());
        model.addAttribute("skillsList", articleService.getSkillsList());
    }

然后使用hibernate二级缓存,如下所示,

and then use hibernate second level cache like below,

    @Entity
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    public class Country {
        ...
    }

类似于技能类

我有三个问题


  1. 将populateModel方法(@ModelAttribute)仅执行一次?即在对ArticleController类执行第一个@RequestMapping方法之前(对于多个会话中的所有请求 - 在启动服务器时,在日志跟踪中看到ArticleController被初始化)?

  2. 我必须做任何事情更多的是我提到了实现二级缓存? (contry list and skills list is purely read only data in two separate tables)

  3. 我想念的任何影响点,你可以咨询。


推荐答案


  1. 将为每个请求调用该方法,如文档。 BTW,如果它只调用一次,它会在哪里找到请求参数(不使用BTW)?

  1. No. The method will be called for every request, as explained in the documentation. BTW, where would it find the request parameter (that you don't use, BTW) if it was called only once?

如果不启用查询缓存除了二级缓存之外,并使查询可以高速缓存,那么Hibernate将会每次执行SQL查询从数据库加载国家ID,然后从二级缓存加载实体本身。如果查询缓存被启用并且查询是可缓存的,则Hibernate将执行单个查询来加载缓存中的所有国家,并且不再执行任何查询(至少对于缓存区域的TTL)

If you don't enable the query cache in addition to the second-level cache, and make the queries cachable, then Hibernate will execute a SQL query to load the country IDs from the database every time, and then load the entities themselves from the second-level cache. If the query cache is enabled and the queries are cachable, then Hibernate will execute a single query to load all the countries in the cache, and won't execute any query anymore afterwards (at least, for the TTL of the cache region)

我想我已经做了我可以做的:-)。您可以阅读以下文章以获得更好的理解。

I think I have already done what I could do :-). You could read the following article for a better understanding.

这篇关于Spring MVC - 从二级缓存中加载参考数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆