多个线程调用@Cacheable方法. Spring cache(3.2.6)允许所有线程进入该方法 [英] Multiple threads calling the @Cacheable method. Spring cache (3.2.6) is allowing all threads into the method

查看:528
本文介绍了多个线程调用@Cacheable方法. Spring cache(3.2.6)允许所有线程进入该方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DAO对象,它具有以下类型的方法.我已将DAO注入服务层,并且能够从此DAO方法调用中获取缓存的结果.但是,当多个线程(在包装DAO单例的代理上)调用此方法时,其中一些线程仍将从我的数据库中获取数据,即fetchDataFromDb()方法调用仍会执行.有办法解决这个问题吗?这是Spring缓存错误吗?

I have a DAO object with a method of the following type. I have injected the DAO into service layer and I'm able to get cached results from this DAO method call. But when multiple threads invoke this method (on a proxy that wraps the DAO singleton) some of those threads still going to fetch the data from my database i.e., the fetchDataFromDb() method call is still executed. Is there a way to get around this? Is this a Spring caching bug?

    @Override
    @Cacheable(value = "CacheName")
    public Map<String, DomainObject> fetchDataFromDb() {
    ....
    }

以下是我的Spring应用程序上下文文件的XML配置.这是一个Web应用程序.我使用JMeter模拟了多个线程.

Following XML configuration of my Spring application context file. This is a web application. I simulated the multiple threads using JMeter.

<cache:annotation-driven />


<!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="CacheName" />              
        </set>
    </property>
</bean>

推荐答案

我只能找到很少的文档,告诉我您所描述的行为是否是错误.但是有一个模糊的暗示,它会出现在(

I could only find little documentation telling me that the behavior you are describing is a bug or not. There is however a vague hint that it would be in the documentation provided at (http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/html/cache.html)

这样,对于一组给定的参数,昂贵的方法(无论是绑定CPU还是IO)只能执行一次,并且可以重复使用结果,而无需再次实际执行该方法

This way, expensive methods (whether CPU or IO bound) can be executed only once for a given set of parameters and the result reused without having to actually execute the method again

可以"在这里是个有问题的词,因为它可能意味着方法执行不能多次执行"或只能执行一次"

The "can" is the problematic word here because it could mean that the method execution "cannot be done more than once" or that it "can be executed just once"

我建议您描述的行为不是错误,而是功能上的缺点".在我看来,防止对同一组参数重复执行该方法似乎是一种编写死锁的简便方法.

I would suggest that the behavior you are describing is not a bug but a functional "shortcoming". Preventing the method to be executed more than once for the same set of parameters seems to me like an easy way to write dead-locks.

我没有答案,我希望有人能纠正我的假设(因为所描述的行为非常有问题).

I do not have the answer to this, and I hope somebody will correct my assumption (because the described behavior is very problematic).

这篇关于多个线程调用@Cacheable方法. Spring cache(3.2.6)允许所有线程进入该方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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