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

查看:129
本文介绍了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)。每当我的应用程序启动时重载webservice

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.

缓存问题。基本上,您是通过webservice检索xml。如果您通过HTTP访问此Web服务,您可以在您的一侧安装简单的HTTP代理服务器来处理xml的缓存。下一步将是在某种本地对象缓存中缓存已解析的xml。每个服务器可以存在此缓存而没有任何问题。在第二种情况下,EHCache将做得很好。在这种情况下,处理链将像这样客户端 - http请求 - > 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代理在与我们的webapp相同的硬件上运行。

  • 可以扩展webapp而无需为xml文件添加新的http代理。

缺点:


  • 下一级基础设施

  • +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天全站免登陆