给定请求的Spring缓存 [英] Spring cache for a given request

查看:125
本文介绍了给定请求的Spring缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC编写Web应用程序.我有一个看起来像这样的界面:

I am writing a web application using Spring MVC. I have a interface that looks like this:

public interface SubscriptionService
{
    public String getSubscriptionIDForUSer(String userID);
}

getSubscriptionIDForUser实际上是通过网络调用另一个服务来获取用户的订阅详细信息.我的业务逻辑在其逻辑中多次调用此方法.因此,对于给定的HTTP请求,我可能对此方法进行了多次调用.因此,我想缓存此结果,以便不会针对同一请求重复进行网络调用.我查看了 Spring文档,但找不到有关如何为同一请求缓存此结果的引用.不用说,如果缓存是对同一userID的新请求,则应将其视为无效.

The getSubscriptionIDForUser actually makes a network call to another service to get the subscription details of the user. My business logic calls this method in multiple places in its logic. Hence, for a given HTTP request I might have multiple calls made to this method. So, I want to cache this result so that repeated network calls are not made for the same request. I looked at the Spring documentation, but could not find references to how can I cache this result for the same request. Needless to say the cache should be considered invalid if it is a new request for the same userID.

我的要求如下:

  1. 对于一个HTTP请求,如果多次调用getSubscriptionIDForUser,则实际方法应仅执行一次.对于所有其他调用,应返回缓存的结果.

  1. For one HTTP request, if multiple calls are made to getSubscriptionIDForUser, the actual method should be executed only once. For all other invocations, the cached result should be returned.

对于其他HTTP请求,即使方法参数完全相同,我们也应该进行新的调用并忽略缓存命中(如果有的话).

For a different HTTP request, we should make a new call and disregard the cache hit, if at all, even if the method parameters are exactly the same.

业务逻辑可能从不同的线程并行执行其逻辑.因此,对于相同的HTTP请求,线程1当前可能正在调用getSubscriptionIDForUser方法,并且在该方法返回之前,线程2还将尝试使用相同的参数来调用相同的方法.如果是这样,则应使线程2等待从线程1发出的调用的返回,而不是再进行另一个调用.从Thread-1调用的方法返回后,Thread-2应该获得相同的返回值.

The business logic might execute its logic in parallel from different threads. Thus for the same HTTP request, there is a possibility that Thread-1 is currently making the getSubscriptionIDForUser method call, and before the method returns, Thread-2 also tries to invoke the same method with the same parameters. If so, then Thread-2 should be made to wait for the return of the call made from Thread-1 instead of making another call. Once the method invoked from Thread-1 returns, Thread-2 should get the same return value.

有指针吗?

更新:我的Web应用程序将部署到VIP后面的多个主机上.我最重要的要求是请求级别缓存.由于每个请求将由单个主机提供服务,因此我只需要将服务调用的结果缓存在该主机中.具有相同userID的新请求不得从缓存中获取该值.我已经浏览了文档,但找不到有关其完成方式的参考.可能是我在看错地方了?

Update: My webapp will be deployed to multiple hosts behind a VIP. My most important requirement is Request level caching. Since each request will be served by a single host, I need to cache the result of the service call in that host only. A new request with the same userID must not take the value from the cache. I have looked through the docs but could not find references as to how it is done. May be I am looking at the wrong place?

推荐答案

EHCache comes to mind right off the bat, or you could even roll-your-own solution to cache the results in the service layer. There are probably a billion options on caching here. The choice depends on several factors, like do you need the values to timeout, or are you going to clean the cache manually. Do you need a distributed cache, like in the case where you have a stateless REST application that is distributed amongst several app servers. You you need something robust that can survive a crash or reboot.

这篇关于给定请求的Spring缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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