- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 Spring 会执行我的自定义 @Cacheable keyGenerator 两次,以用于单次调用注释为 @Cacheable 的方法,为什么不只执行一次。
我的 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();
}
}
我的方法用带有自定义 keyGenerator 的 @Cacheable 注释
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 注释的方法进行一次调用时,我在我的日志和调试器中看到了 2 次执行 System.out.println("生成 key ");
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,首先使用它来查找缓存,如果没有找到,当方法完成时,使用相同的键放入缓存。所以我不明白为什么我会看到它运行两次。
我的问题:
最佳答案
我想我知道为什么了,key 生成一次用于在缓存中查找,一次用于放入缓存。不确定为什么会这样,但似乎是正在发生的事情。
关于java - Spring为什么单次调用@Cacheable注解方法会执行2次@Cacheable keyGenerator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51995717/
我想知道我是否可以实例化 javax.crypto.KeyGenerator仅一次,然后在多线程环境中使用此实例。 它的 JavaDoc 文档没有说明它的线程安全性。或者使用 ThreadLocal
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 5 年前。 Improve th
当我的应用程序尝试从 JAVA 加载 RSA 算法提供程序类时遇到错误。异常堆栈如下: javax.jms.JMSException: RSA premaster secret error at or
为什么 Spring 会执行我的自定义 @Cacheable keyGenerator 两次,以用于单次调用注释为 @Cacheable 的方法,为什么不只执行一次。 我的 KeyGenerator
我正在向我的 Web 应用程序添加缓存机制。经过研究后,我决定将 Ehcache 与 Spring 一起使用。 向方法添加@Cachable注释将执行该方法一次,并且对于任何进一步的调用,将从缓存返回
RandomGenerator 应该为每个唯一种子生成唯一 key ,这是否正确? 我编写了以下代码,但对于相同的种子,它生成不同的字节数组 - 为什么? public static byte[] g
我需要在 Java 中实现 DES,我看到了有时使用 KeyGenerator 有时使用 SecretKeyFactory 生成 key 的示例,两者似乎都有效。 问题是哪种方法更好或者它们是否相同?
代码非常简单明了,但是在处抛出了异常 KeyGenerator keyGen = KeyGenerator.getInstance("RC4"); 和 Cipher aesCipher = Ciphe
我正在编写一个在 DES 中进行加密和解密的程序。加密过程中使用的相同 key 也应该在解密时使用,对吗?我的问题是加密和解密在不同的机器上运行。这就是在加密过程中生成 key 的方式。 Secret
我正在使用带有 Redis 的 spring 缓存进行缓存 我有以下方法: @CachePut(value ="DATA1", key = "#key1") public Object
我是 Android 开发新手,我尝试生成用于加密和解密的 key 并将该 key 存储在 key 存储中。但是在运行应用程序时出现以下错误。 java.security.NoSuchAlgorith
我有一个并发加密/解密程序,其中通过调用以下代码(用scala编写,Java版本应该很相似)同时随机生成多个AES128 key : private def AESKeyGen: KeyGener
我正在使用 Spring 3.1,我想使用新的缓存功能。然后,我尝试了: 但是我没有找到配置自定义 KeyGenerator 的方法。有什么想法吗? 最佳答案 Spring 3.1 RC1 中
【现象】 javax.net.ssl.SSLKeyException: RSA premaster secret error at sun.security.ssl.RSAClient
我已经问过这个问题了here但我觉得 Stackoverflow 可能会更快。这就是我尝试在我的 json 配置文件中执行此操作的方式: { "Serilog": { "Using": [
我正在编写一个 Web 服务,它允许用户从算法列表中选择一个算法并生成指定大小的 key (使用 KeyGenerator)。我无法通过 API 找到任何方法来获取指定算法的一组可能的 key 大小。
当我的缓存键在 Spring 中使用 @Cacheable 注释发生冲突时,我遇到了问题。例如,使用以下两种方法: @Cacheable("doOneThing") public void doOne
我对为什么在生成 key 加密时需要指定算法(例如“AES”)感到困惑,例如... KeyGenerator kg = KeyGenerator.getInstance("AES"); 它显然不用于指
以下所有结果都在 "java.security.NoSuchAlgorithmException: SHA not SecureRandom available" (或类似)。 SecureRando
我们目前正在尝试使用生物识别技术对数据进行签名,我们使用 androidx.biometric 库。 问题是,如果没有安装指纹但人脸已注册,我们无法生成任何 key 。 仅使用指纹或结合指纹和面部,一
我是一名优秀的程序员,十分优秀!