简体   繁体   中英

Spring Cache not working in doFilter

Spring Cache not working if called from doFilter.

Note that Spring cache is working if NOT called from doFilter() (eg if called from rest service)

How can I enable cache in doFilter() ? (maybe cache is not allowed in doFilter?)

@Configuration
@EnableCaching
public class CredentialsInjectionFilter implements javax.servlet.Filter {

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(
                new ConcurrentMapCache("tenants")               
            ));
        return cacheManager;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
    throws IOException, ServletException {
        display(5);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Cacheable("tenants")
    public void display(int number) {
        // if cache working properly, code below will not execute after the first calling
        for(int i=0; i<50; i++) {   
               System.out.println("ramon called" +number);
        }
    }

How about extending org.springframework.web.filter.GenericFilterBean, and moving the caching details into a separate service class

CacheService

import java.util.Arrays;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

    @Component
    public class CacheService{

        @Bean
        public CacheManager cacheManager() {
            SimpleCacheManager cacheManager = new SimpleCacheManager();
            cacheManager.setCaches(Arrays.asList(
                    new ConcurrentMapCache("tenants")               
                ));
            return cacheManager;
        }


        @Cacheable("tenants")
        public void display(int number) {
            // if cache working properly, code below will not execute after the first calling
            for(int i=0; i<50; i++) {   
                   System.out.println("ramon called" +number);
            }
        }
    }

CredentialsInjectionFilter

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.filter.GenericFilterBean;

@Component
public class CredentialsInjectionFilter extends GenericFilterBean {

    @Autowired
    private CacheService cacheService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
    throws IOException, ServletException {
        cacheService.display(5);
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM