【发布时间】:2018-07-17 16:18:01
【问题描述】:
我在 Spring Boot 应用程序中使用了 Spring 缓存来针对某个键存储值。我现在有了值,是否可以根据值从缓存中获取密钥?如果是这样请帮助。
我尝试使用有关 net.sf.ehcache.Cache 的解决方案,但由于某种原因,它没有显示任何导入建议并给出错误 net.sf.ehcache.Cache 无法解析为类型。我是弹簧缓存的新手,所以不知道该怎么做。
我项目中的依赖是
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
我使用的代码是
public String getEmailByOtp(String otp)
{
String email = "";
Ehcache cache = (Ehcache) CacheManager.getCache("otpCache").getNativeCache();
for (Object key: cache.getKeys()) {
Element element = cache.get(key);
if (element != null) {
Object value = element.getObjectValue(); // here is the value
if(value.equals(otp)) {
email = key.toString();
}
}
}
return email;
}
【问题讨论】:
标签: java spring spring-boot ehcache spring-cache