我想为Spring Cache执行以下操作。
-
检查传入的字符串是否存在于缓存中。如果存在则返回true,如果不存在则添加到缓存中;checkInCache (String str)
-
从缓存中删除字符串驱逐(String str)
尝试如下
@ componentFlightCache {
public static final Logger log = LoggerFactory.getLogger(FlightCache.class);
@Autowired
CacheManager cacheManager;
public boolean isFlightKeyPresent(final String flightKey) {
final ValueWrapper existingValue = cacheManager.getCache("flightCache").get(flightKey);
log.info("existingValueexistingValue " + existingValue);
if (existingValue == null) {
cacheManager.getCache("flightCache").put(flightKey, flightKey);
return false;
} else {
return true;
}
}
并在配置类上添加了@EnableCaching注释。
错误:
required a bean of type 'org.springframework.cache.CacheManager' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)Action:Consider defining a bean of type 'org.springframework.cache.CacheManager' in your configuration.
-
要检查缓存中是否包含键,可以这样做:
@ autowired缓存管理器缓存管理器;
boolean isKeyPresent(对象键){cacheManager.getCache("MyCacheName").get(key) != null;}
-
退出键:
@ autowired缓存管理器缓存管理器;
boolean cacheEvict(对象键){cacheManager.getCache("MyCacheName" .evictIfPresent(关键);}