为什么Spring对@Cacheable注释方法的单次调用执行两次自定义@Cacheable keyGenerator,为什么不执行一次。
我的KeyGenerator实现
package com.example.demo;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* Custom key generator
*/
@Component(value = "customerKeyGenerator")
public class CustomerKeyGen implements KeyGenerator
{
@Override
public Object generate(Object target, Method method, Object... params)
{
System.out.println("Generating a key");
ArrayList<String> customerNames = (ArrayList<String>) params[0];
return customerNames.hashCode();
}
}
我的方法使用@Cacheable使用自定义keyGenerator进行注释
package com.example.demo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@Component
public class CustomerService {
@Cacheable(value = "customersCache", keyGenerator = "customerKeyGenerator")
public int getCountOfCustomers(ArrayList<String> customerNames) {
return customerNames.size();
}
}
Spring Rest Controller调用@Cacheable注释的方法
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
@Controller
public class CustomerController {
@Autowired
CustomerService customerService;
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
@RequestMapping("/countCustomers")
@ResponseBody
String countCustomers() {
ArrayList<String> customerNames = new ArrayList<>();
customerNames.add("john");
customerNames.add("bill");
return "countOfCustomers=" + String.valueOf(customerService.getCountOfCustomers(customerNames));
}
}
当我使用自定义keyGenerator对使用@Cacheable注释的方法进行单个调用时,我在我的日志和System.out.println的dubugger中看到了2个执行(“生成密钥”);
Curl触发方法调用调用
curl http://127.0.0.1:8080/countCustomers
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18 100 18 0 0 18 0 0:00:01 --:--:-- 0:00:01
76countOfCustomers=2
日志我在应用程序属性中具有以下设置以启用对缓存的跟踪
logging.level.org.springframework.cache = TRACE
...
2018-08-27 11:56:53.753 INFO 18756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-08-27 11:56:53.757 INFO 18756 --- [ main] c.example.demo.TestCacheableApplication : Started TestCacheableApplication in 3.543 seconds (JVM running for 5.137)
2018-08-27 11:56:58.411 INFO 18756 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-08-27 11:56:58.411 INFO 18756 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-08-27 11:56:58.446 INFO 18756 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 35 ms
Generating a key
2018-08-27 11:56:58.480 TRACE 18756 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : Computed cache key '104328221' for operation Builder[public int com.example.demo.CustomerService.getCountOfCustomers(java.util.ArrayList)] caches=[customersCache] | key='' | keyGenerator='customerKeyGenerator' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2018-08-27 11:56:58.480 TRACE 18756 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : No cache entry for key '104328221' in cache(s) [customersCache]
Generating a key
2018-08-27 11:56:58.480 TRACE 18756 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : Computed cache key '104328221' for operation Builder[public int com.example.demo.CustomerService.getCountOfCustomers(java.util.ArrayList)] caches=[customersCache] | key='' | keyGenerator='customerKeyGenerator' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
从概念上讲,我认为Spring只需要运行一次keyGenerator,首先使用它来查找缓存,如果没有找到,则在方法完成时,使用相同的密钥放入缓存。所以我不明白为什么我看到这两次运行。
我的问题是:
我想我知道为什么,密钥生成一次用于在缓存中查找,一次用于放入缓存。不确定为什么它会这样运作,但似乎正在发生的事情。