0

我添加了所有依赖项,使用 bean 定义创建配置类并将属性添加到 application.yaml。我以这种方式定义了 bean,因为我需要在我的应用程序中使用 javax.cache.CacheManager。

请帮助我理解为什么没有创建具有给定 ID 的缓存。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>${caffeine.version}</version>
</dependency>
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>jcache</artifactId>
    <version>${caffeine.version}</version>
</dependency>
<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
</dependency>

---

spring:
  cache:
    type: caffeine
    cache-names: allowed-ids

---

@Configuration
@EnableCaching
public class CacheConfig {

  @Bean
  public CacheManager cacheManager() {
    CachingProvider caffeineCachingProvider =
        Caching.getCachingProvider(CaffeineCachingProvider.class.getName());
    return caffeineCachingProvider.getCacheManager();
  }
}

---

private void updateDictionaryIds(@NonNull Set<Dictionary> dictionaries) {
    cacheManager.getCache(CACHE_NAME).removeAll(); // <--- NPE
    dictionaries.stream()
        .map(Dictionary::getDictionaryId)
        .forEach(id -> {
          Cache<String, String> cache = cacheManager.getCache(CACHE_NAME);
          cache.put(id, Const.EMPTY);
        });
  }

public @NonNull Set<String> getAllowedIds() {
    Cache<String, String> cache = cacheManager.getCache(CACHE_NAME, String.class, String.class);
    return StreamSupport.stream(cache.spliterator(), false)
        .map(Cache.Entry::getKey)
        .collect(Collectors.toSet());
  }
4

0 回答 0