Java Web 应用程序:如何实现缓存技术? [英] Java Web Application: How to implement caching techniques?

查看:23
本文介绍了Java Web 应用程序:如何实现缓存技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Java Web 应用程序,该应用程序的行为基于从 Web 服务加载的大型 XML 配置文件.由于在访问应用程序的特定部分之前实际上不需要这些文件,因此它们被延迟加载.当需要这些文件之一时,会向 Web 服务发送查询以检索相应的文件.由于某些配置文件的使用频率可能比其他文件高得多,因此我想设置某种缓存(可能有 1 小时的到期时间)以避免一遍又一遍地请求相同的文件.

I am developing a Java web application that bases it behavior through large XML configuration files that are loaded from a web service. As these files are not actually required until a particular section of the application is accessed, they are loaded lazily. When one of these files are required, a query is sent to the webservice to retrieve the corresponding file. As some of the configuration files are likely to be used much, much more often than others I'd like to setup some kind of caching (with maybe a 1 hour expiration time) to avoid requesting the same file over and over.

Web 服务返回的文件对于所有会话中的所有用户都是相同的.我不使用 JSP、JSF 或任何其他花哨的框架,只使用普通的 servlet.

The files returned by the web service are the same for all users across all sessions. I do not use JSP, JSF or any other fancy framework, just plain servlets.

我的问题是,在 Java Web 应用程序中实现这种全局静态缓存的最佳实践是什么?单例类是否合适,还是会因为 J2EE 容器而出现奇怪的行为?我应该通过 JNDI 在某处公开一些东西吗?我应该怎么做才能使我的缓存在集群环境中不会被搞砸(可以,但没有必要,每个集群服务器有一个缓存)?

My question is, what is considered a best practice to implement such a global, static cache within a java Web application? Is a singleton class appropriate, or will there be weird behaviors due to the J2EE containers? Should I expose something somewhere through JNDI? What shall I do so that my cache doesn't get screwed in clustered environments (it's OK, but not necessary, to have one cache per clustered server)?

根据以上信息,将负责缓存的对象作为 ServletContext 属性是否是正确的实现?

Given the informations above, Would it be a correct implementation to put an object responsible for caching as a ServletContext attribute?

注意:我不想在启动时加载所有这些并完成它,因为那样会

Note: I do not want to load all of them at startup and be done with it because that would

1).每当我的应用程序启动时都会使网络服务过载
2).我的应用程序运行时文件可能会更改,因此无论如何我都必须重新查询它们
3).我仍然需要一个全局可访问的缓存,所以我的问题仍然存在

1). overload the webservice whenever my application starts up
2). The files might change while my application is running, so I would have to requery them anyway
3). I would still need a globally accessible cache, so my question still holds

更新:使用缓存代理(例如squid)可能是个好主意,但是对webservice 的每个请求都会在post Data 中发送相当大的XML 查询,每次可能会有所不同.只有 Web 应用程序真正知道对 Web 服务的两次不同调用实际上是等效的.

Update: Using a caching proxy (such as squid) may be a good idea, but each request to the webservice will send rather large XML query in the post Data, which may be different each time. Only the web application really knows that two different calls to the webservice are actually equivalent.

感谢您的帮助

推荐答案

您的问题包含几个单独的问题.让我们慢慢开始.ServletContext 是您可以存储缓存句柄的好地方.但是您通过为每个服务器实例设置缓存来付费.应该没有问题.如果您想注册更大范围的缓存,请考虑将其注册到 JNDI 中.

Your question contains several separate questions together. Let's start slowly. ServletContext is good place where you can store handle to your cache. But you pay by having cache per server instance. It should be no problem. If you want to register cache in wider range consider registering it into JNDI.

缓存问题.基本上,您是通过网络服务检索 xml.如果您通过 HTTP 访问此 Web 服务,您可以在您的一侧安装简单的 HTTP 代理服务器来处理 xml 缓存.下一步将是在某种本地对象缓存中缓存解析的 xml.这个缓存可以存在于每台服务器上,没有任何问题.在这第二种情况下,EHCache 将做得很好.在这种情况下,处理链将像这样 Client - http request ->servlet ->查看本地缓存 - 如果没有缓存 ->查看 http 代理(xml 文件)->做代理工作(http 到 webservice).

The problem with caching. Basically, you are retrieving xml via webservice. If you are accesing this webservice via HTTP you can install simple HTTP proxy server on your side which handle caching of xml. The next step will be caching of resolved xml in some sort of local object cache. This cache can exists per server without any problem. In this second case the EHCache will do perfect job. In this case the chain of processing will be like this Client - http request -> servlet -> look into local cache - if not cached -> look into http proxy (xml files) -> do proxy job (http to webservice).

优点:

  • 每个服务器实例的本地缓存,仅包含来自请求的 xmls 的对象
  • 一个 http 代理在与我们的 web 应用相同的硬件上运行.
  • 无需为 xml 文件添加新的 http 代理即可扩展 Web 应用程序.

缺点:

  • 下一级基础设施
  • +1 点故障(http 代理)
  • 更复杂的部署

更新:不要忘记始终将 HTTP HEAD 请求发送到代理以确保缓存是最新的.

Update: don't forget to always send HTTP HEAD request into proxy to ensure that cache is up to date.

这篇关于Java Web 应用程序:如何实现缓存技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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