何时使用缓存/二级缓存?有具体的实际场景吗?

发布于 2024-09-17 18:44:24 字数 678 浏览 3 评论 0原文

我正在开发一个基于 Web 的应用程序,该应用程序属于一家汽车制造商,该应用程序是使用 MS SQL Server 2005 数据库在 Spring-Hibernate 中开发的。

通过此应用程序,最终用户可以通过基于 Web 的界面请求创建汽车、公共汽车、卡车等。当用户登录时,会显示一个 HTML 表单,用于捕获车辆的技术规格,例如,如果有人想要请求汽车,他可以指定发动机品牌/型号、轮胎、底盘详细信息等。总共有 100 个表单元素在“创建车辆请求”屏幕上,其中 30% 是用于显示选项的下拉菜单(选择框)(即允许用户选择其中之一)。这些选择框由数据库(主数据)中存储的值填充。通过在后端运行存储过程,该主数据每周至少更改一次。

该应用程序在全球大约有 10,000 名用户,我们预计新车请求每天最多有 5000 次点击,即将显示 5000 次创建车辆表单。

我的问题是,我是否需要使用二级缓存选项来存储从主数据显示的表单字段的值?

由于这些值是从一组每周更改一次的主表中显示的,因此我认为缓存主数据将有助于提高性能,但我不太确定,因为我还没有将我的应用程序移动到生产以查看真实性能并查看我是否真的需要缓存。

如果我选择缓存,我可能需要花一两周的时间来弄清楚如何配置它,而我不想花一两周的时间却看不到任何真正的好处?

需要专家帮忙解决这个问题。另外,如果有人可以分享真正需要缓存的实际场景,将会有很大的帮助。

I'm working on a web based application that belongs to an automobil manufacturer, developed in Spring-Hibernate with MS SQL Server 2005 database.

Through this application, end users can request for creating a Car, Bus, Truck etc through web based interfaces. When a user logs in, a HTML form gets displayed for capturing technical specification of vehicle, for ex, if someone wanted to request for Car, he can speify the Engine Make/Model, Tire, Chassis details etc. There are overall 100 form elements on Create Vehicle Request screen, out of which 30% are drop downs (SELECT boxes) for displaying options (i.e. users are allowed to choose one of them) . These SELECT boxes gets populated from values stored in database (Master data). This master data changes at least once a week by running a stored procedure in back-end.

There are approximately 10,000 users for this application globally and at the most we are expecting 5000 hits everyday for new vehicle request, i.e. 5000 times Create vehicle form will be displayed.

My question is, Do i need to go with Second level cache option for storing the values for form field getting displayed from master data?

Since these values are getting displayed from a set of Master tables that will change once a week only, I'm thinking caching of master data will help improving performance, but I'm not too sure as I've yet to move my application to Production to see the real performance and to see, if i really need caching.

If I go with caching, I might need to spent a week or two figuring out how to configure that and I don't wanted to spend a week or two without seeing any real benefit?

Need experts help on this. Also if someone can share practical scenarios, where caching is really needed would be of great help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不顾 2024-09-24 18:44:24

我的问题是,我是否需要使用二级缓存选项来存储从主数据显示的表单字段的值?

不需要,但您可以。并且只读数据(或大部分读取),例如不可变的参考数据(国家、州、税码,或者在您的情况下,发动机、轮胎、底盘等)是第二级的完美候选者缓存(以及查询缓存,如果需要)。

由于这些值是从一组每周仅更改一次的主表中显示的,因此我认为缓存主数据将有助于提高性能,

好吧,这取决于硬件数量、集群大小等,应用程序可能只能处理负载。但正如我所写,缓存只读数据是很常见的:

  • 每次都访问数据库没有真正的意义
    • 从长远来看,避免这些打击不会有什么坏处,即使现在这不是问题
  • ,它们不会经常更改,缓存它们很容易处理并且效果很好,

请记住,缓存意味着它们的对象表示不会被垃圾收集,因此可能会稍微增加内存需求。

但我不太确定,因为我还没有将我的应用程序移至生产环境以查看真实性能并查看我是否真的需要缓存。

老实说,您不应该等到生产时才查看是否存在性能问题。您应该先在专用环境中加载或压力测试您的应用程序,并调整您的应用程序、JVM、应用程序服务器、数据库等。并且不要忘记,您无法改进无法衡量的内容

如果我选择缓存,我可能需要花一两周的时间来弄清楚如何配置它,而我不想花一两周的时间却看不到任何真正的好处?

事情并没有那么复杂。您需要在 Hibernate 配置中激活二级缓存和查询缓存并选择缓存提供程序。我的建议是使用 EhCache,相关属性是:

hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

然后,将相关实体标记为可缓存(如果不按 id 加载数据,则缓存用于检索数据的查询)。

如果您决定使用二级缓存,则必须手动逐出缓存,因为您不是通过 Hibernate API 更新数据,而是使用存储过程(使用 evict 方法) SessionFactory)。这将需要一些代码行。如果可能的话,重新启动您的应用程序将是另一种选择。

My question is, Do i need to go with Second level cache option for storing the values for form field getting displayed from master data?

You don't need to, but you could. And and read-only data (or mostly read) such as immutable reference data (countries, states, tax codes or, in your case, Engine, Tire, Chassis, etc) are perfect candidates for second level caching (and query caching if required).

Since these values are getting displayed from a set of Master tables that will change once a week only, I'm thinking caching of master data will help improving performance,

Well, depending on how much hardware, the size of your cluster, etc, your app might just be able to handle the load. But as I wrote, it's very common to cache read-only data:

  • there is no real point at hitting the database each time
    • avoiding these hits can't hurt if on the long run, even if it's not a problem now
  • they don't change often, caching them is easy to handle and works very well

Just keep in mind that caching things means that their object representation won't be garbage collected so it might increase the memory needs a bit.

but I'm not too sure as I've yet to move my application to Production to see the real performance and to see, if i really need caching.

Honestly, you should not wait until production to see if there is a performance problem. You should load or stress test your application before in a dedicated environment and tune your application, the JVM, the app server, the database, etc. And don't forget, you can't improve what you can't measure.

If I go with caching, I might need to spent a week or two figuring out how to configure that and I don't wanted to spend a week or two without seeing any real benefit?

It is not that complicated. You need to activate second level caching and query caching in the Hibernate configuration and to choose a caching provider. My recommendation would be to use EhCache and the relevant properties are:

hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

Then, mark the relevant entities as cacheable (and cache the queries used to retrieve the data if you don't load them by id).

And if you decide to use the second level cache, you'll have to evict the cache manually since you're not updating the data through Hibernate API but using a stored procedure (with the evict methods on the SessionFactory). This will require some lines of code. If this is possible, restarting your app would be another option.

疯狂的代价 2024-09-24 18:44:24

每天 5000 次点击听起来像是很多流量,但请记住,这只是每分钟 3 次半点击。假设您的数据库可以在几百毫秒内执行呈现页面所需的所有查询,那么您可能没问题。

也就是说,缓存是下一步的不错选择。听起来查询缓存可能对您有用,因为您的数据很少更改。

5000 hits per day sounds like a lot of traffic, but keep in mind that this is only 3 and a half hits per minute. Assuming your database can perform all of the queries needed to render the page in under a couple hundred milliseconds, you're probably fine.

That said, caching is a fine next step. It sounds like query caching might be useful for you, since your data changes so rarely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文