本文介绍了在Spring MVC Rest服务中缓存HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring MVC rest服务,该服务以XML返回数据。我想缓存此xml响应。我该如何实现?可以使用mvc:interceptors来做到这一点吗?

I have a spring MVC rest service that returns data in XML. I would like to cache this xml response. How can I achieve this? Is it possible to do this using mvc:interceptors?

推荐答案

您可以做到这一点,但是我认为有更好的解决方案。

You could make this work, but I think there are better solutions.

首先,如果要使用Spring MVC拦截器,则将使用postHandle方法将某些内容存储在缓存中,并使用preHandle来检查缓存和可能的绕行处理。问题是,您将什么存储在缓存中。您将需要存储完整的响应。这意味着您必须在postHandle中轻松地从ModelAndView获得完整的响应。这可能很容易,也可能不容易,具体取决于您的工作方式。

First, if you want to use Spring MVC interceptors, you'll use the postHandle method to store something in your cache and the preHandle to check the cache and possible circumvent processing. The question is, what do you store in the cache. You would need to store the complete response. This means that you would have to easily get the full response from your ModelAndView in postHandle. This may or may not be easy, depending on how you're doing things.

您很可能最好同时使用不同的缓存机制。我建议在Web服务器级别进行缓存。如果您要缓存在拦截器级别,尤其是这样,因为它正好位于Web服务器的下一个,并且在此重新发明轮子,我看不出任何好处。 Apache有一个缓存模块。 Nginx也是如此。清漆也很棒。

You're most likely better off using a different caching mechanism all together. I recommend caching at the web server level. This is especially true if you're looking to cache in the interceptor level as that is right "next" to the web server and I don't see any benefit in re-inventing the wheel there. Apache has a cache module. So does nginx. Varnish is pretty awesome too.

我还应该提到,在确定需要缓存(不要过早优化)之前,不要缓存。这是浪费您的时间和精力。其次,当确定确实存在需要解决的性能问题(并且缓存是正确的解决方案)时,应在正确的位置缓存正确的数据。

I should also mention that you should not cache until you've determined that you need to (don't prematurely optimize). This is a waste of your time and effort. Secondly, when you've determined that you do have performance issues that need to be fixed (and caching is the correct solution), you should cache the right data in the right place.

现在,假设您确定确实存在性能问题,并且某种缓存是一个很好的解决方案。接下来要确定的是可以缓存的内容。如果对于每个URL,您返回相同的数据,那么最好在Web服务器(Apache,nginx,Varnish等)级别进行缓存。

Now, say you've determined that you do have a performance problem and some sort of caching is a good solution. The next thing to determine is what can be cached. If, for every URL, you return the same data, then caching at the web server (Apache, nginx, Varnish, etc.) level will be your best bet.

通常,您会遇到两个客户端访问同一URL并获取不同数据的情况。在像Facebook这样的网站上最容易看到这一点。登录后,我看到的数据与朋友看到的数据不同。在这种情况下,您将无法在Web服务器级别进行缓存。您将需要在应用程序内部进行缓存。通常,这意味着在数据库级别进行缓存。

Often, you will have cases where two clients will hit the same URL and get different data. This is most easily seen on a site like Facebook. I see different data when I'm logged in than my friend sees. In this case, you will not be able to cache at the web server level. You will need to cache inside your application. Usually this means caching at the database level.

这篇关于在Spring MVC Rest服务中缓存HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:19