缓存在 spring 拦截器中不起作用
caching is not working in spring interceptor
我正在使用拦截器对用户进行身份验证,我希望它可以使用 Redis 进行缓存。下面是代码。缓存在拦截器内部不起作用。 @Cacheable
在我的控制器中工作 我已经测试过
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import uk.co.rullion.ledger.exception.UnauthorisedAccessException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
@Component
@Profile({"dev","prod"})
public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object object) throws UnauthorisedAccessException {
Map<String,List> userRoles = authenticateUser(username, password);
....
return authorizationResult;
}
@Cacheable(value="authenticateUser")
public Map<String,List> authenticateUser(String username, String password) {
//authenticating user logic
// there service layer call is happening everytime
return userRoles;
}
}
Only external method calls coming in through the proxy are
intercepted. This means that self-invocation, in effect, a method
within the target object calling another method of the target object,
will not lead to an actual cache interception at runtime even if the
invoked method is marked with @Cacheable.
将 authenticateUser 方法调用提取到单独的服务中,并将 class 声明为 @Service/@Component 并且将使用 @cacheable 注释缓存单个方法
我正在使用拦截器对用户进行身份验证,我希望它可以使用 Redis 进行缓存。下面是代码。缓存在拦截器内部不起作用。 @Cacheable
在我的控制器中工作 我已经测试过
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import uk.co.rullion.ledger.exception.UnauthorisedAccessException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
@Component
@Profile({"dev","prod"})
public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object object) throws UnauthorisedAccessException {
Map<String,List> userRoles = authenticateUser(username, password);
....
return authorizationResult;
}
@Cacheable(value="authenticateUser")
public Map<String,List> authenticateUser(String username, String password) {
//authenticating user logic
// there service layer call is happening everytime
return userRoles;
}
}
Only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual cache interception at runtime even if the invoked method is marked with @Cacheable.
将 authenticateUser 方法调用提取到单独的服务中,并将 class 声明为 @Service/@Component 并且将使用 @cacheable 注释缓存单个方法