Announcement Announcement Module
Collapse
No announcement yet.
@CacheEvict is never invoked Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @CacheEvict is never invoked

    I am using Spring 3.1.0-RELEASE version. I am caching an object and evicting when an operation is made that changes the state of this object (so that I can fetch a fresh copy of this object upon next request). I am caching in my Dao tier as follows:

    Code:
        
    @Cacheable(value = "someNamespacedCacheName", key = "#someParam.someAttribute")
    public SomeObject retrieveSomeObject(ParamNeededForSomeObject someParam) {
    //do something to retrieve SomeObject
    }
    I am evicting on the same cache as follows:

    Code:
        @CacheEvict(value = "someNamespacedCacheName", beforeInvocation = true, key = "#uniqueIdForSomeObject")
        public boolean updateSomeObject(String uniqueIdForSomeObject) {
            //do something to update SomeObject 
        }
    I can see by looking at the logs that Spring evicts the cache as expected. However, I also have to remove this cached object upon user signing out from the application. A class (lets call it GuestLogoutEventListener) is notified by a regular filter (not managed by spring) that guest wants to logout. I have another evict configured on GuestLogoutEventListener (this is spring managed. The filter gets hold of the application context and gets hold of this bean by name and calls the notification method). The evict on this notification operation is configured as follows. This evict NEVER works.

    Code:
     
    public class GuestLogoutEventListener  {
       @CacheEvict(value = "someNamespacedCacheName", beforeInvocation = true, key = "#uniqueIdForSomeObject")
        public void guestWantsToLogout(String uniqueIdForSomeObject) {
            //do something to clear the guest work space
        }
    }
    The bean GuestLogoutEventListener is configured in the application's context as follows:

    Code:
        <bean id="guestLogoutEventListener  " class="xx.xx.xx.xx.GuestLogoutEventListener"/>

    The filter gets hold of this bean as follows:

    Code:
    @Override
        public void init(FilterConfig filterConfig) throws ServletException {
     ApplicationContext context = this.getContext(filterConfig.getServletContext());
            if (context != null) {
                this.uhfApplicationContext = (UHFApplicationContext) context.getBean("uhfApplicationContextBean");
            }
    }
    
        protected ApplicationContext getContext(ServletContext servletContext) {
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            if (context == null) {
                LOG.error("Spring Application context is NULL!");
            }
            return context;
        }
    the applicationContextBean is configured as follows inside the web application's context

    Code:
      <bean id="uhfApplicationContextBean" class="xx.xx.xx.xx.UHFApplicationContext">
        //other beans ignored for simplicity
        <property name="guestLogoutEventListener" ref="guestLogoutEventListener " />
    
      </bean>
    I can see the notification method being called but the cache is not being evicted. What am I missing ? Why is only this cache evict not being invoked?
Working...
X