从其他控制器调用方法时,@ cacheEvict无法正常工作 [英] @cacheEvict not working while calling the method from different controller

查看:52
本文介绍了从其他控制器调用方法时,@ cacheEvict无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在春季应用中,我们有两个控制器,即controller1,controller2和一个服务,即service1

In spring application we have two controllers i.e. controller1, controller2 and one services i.e. service1

我想使用方法缓存,为此,我已经配置了spring缓存.

i want to use method caching and for that i have configured spring cache.

我正在使用 @Cacheable(value ="cache1")在service1中缓存名为method1的方法,并且为了删除缓存,我正在使用 @CacheEvict(value ="cache1",allEntries = true)在service1中另一个名为method2的方法上.

i am caching method named method1 in service1 with @Cacheable(value = "cache1") and for removing cache i am using @CacheEvict(value = "cache1", allEntries = true) on another method named method2 in service1.

所以缓存可以正常工作,但是驱逐无法按我想要的方式工作.

so caching works fine but evicting not working as i want.

因此,如果我从controller1调用method1(可缓存方法),它将缓存并从controller1调用method2(cacheEvict方法),那么当我从controller2调用method2(cacheEvict方法)时,它将正确地删除/逐出缓存,但它没有被移除/取消缓存,但我想这样做.

so if i call method1 (cachable method) from controller1 it will cache and call method2 (cacheEvict method) from controller1 then it will remove / evict cachce properly BUT when i call method2 (cacheEvict method) from controller2 then it is NOT REMOVING / EVICTING CACHE but I WANT IT TO DO.

无论何时从哪个控制器调用该方法,我都希望逐出/删除缓存.

在大多数情况下这是必需的,因为controller1用于客户,而controller2用于管理员.我们会缓存controller1中使用的方法,并希望在controller2中发生任何更新(即admin)时删除相同的缓存.

this is required in most cases because controller1 is for customer and controller2 is for admin. we cache methods used in controller1 and we want to remove the same cache when any update happens from controller2 i.e. admin.

推荐答案

两个要点使这个工作出错.他们是:

Two main points that make this works wrong. They are :

  1. 在Controller中,我们使用注释 @autowire ;在服务中,我们使用 xml bean配置.所以两者都创建了不同的实例,这使它无法正常工作.

  1. In Controller we are using annotation @autowire and in services we are using xml bean configuration. so both are creating different instance and it makes it works wrong.

解决方案:对于控制器,我们在xml中的定义方式也与对服务的定义方式相同.

Solution : for controller also we have defined in xml the same way we do for services.

我们在春季而不是在春季的DispatcherServlet中集成了dwr(directwebremoting),因此它可以独立工作,因此服务实例也有所不同.

we integrated dwr (directwebremoting) in spring but not in spring's DispatcherServlet so it works but independently so for that also services instance are different.

解决方案:按照dwr站点中的定义,将dwr集成到DispatcherServlet中,以与springMVC应用程序集成(我们已经采取另一种方式)

Solution : Integrated dwr inside DispatcherServlet as definded in dwr site to integrate with springMVC application (we have done another way)

这篇关于从其他控制器调用方法时,@ cacheEvict无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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