java - 创建缓存时如何将所有缓存名称及其数据放入List<>中?

标签 java caching jakarta-ee ehcache ehcache-3

我已经使用过 Ehcache 3,并且我是 Ehcache 3 的新手,并且我有一个缓存管理器,如下所示:

 private CacheManager cacheManager;

现在,当创建对象时,cacheManager 被初始化:

public ListPlanImpl() {
        System.out.println("constructore being initalized");
        System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
        cacheManager = CacheManagerBuilder
                .newCacheManagerBuilder().build();
        cacheManager.init();


    }

初始化cacheManager后,这是我的主类,所有获取和放入缓存的操作都在其中发生。我已将多个数据插入到不同的缓存中,例如:

     @Stateless
        public class ListPlanImpl implements CachePlan,ListPlan {

         private static final String CACHE_OPERATING_PARAMETER = "cache_key_operating_parameter";
            private static final String CACHE_SECURITY_PARAMETER = "cache_security";
      private static Cache<String, GenericClassForList> operatingParametersCache;
        private static Cache<String, GenericClassForList> securitiesTradingParameterCache;

         public void putInCache() throws ExecutionException, InterruptedException {
                System.out.println("putting in list");

                this.operatingParametersCache = cacheManager
                        .createCache("cacheOfOperatingParameters", CacheConfigurationBuilder
                                .newCacheConfigurationBuilder(
                                        String.class, GenericClassForList.class,
                                        ResourcePoolsBuilder.heap(1000000000)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60000,
                                        TimeUnit.SECONDS))));

                operatingParametersCache.put(CACHE_OPERATING_PARAMETER, new GenericClassForList(this.operatingParamService.getOperatingParamDTO()));


                this.securitiesTradingParameterCache = cacheManager
                        .createCache("cacheOfSecurityTrading", CacheConfigurationBuilder
                                .newCacheConfigurationBuilder(
                                        String.class, GenericClassForList.class,
                                        ResourcePoolsBuilder.heap(1000000000)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60000,
                                        TimeUnit.SECONDS))));
  securitiesTradingParameterCache.put(CACHE_SECURITY_PARAMETER, new GenericClassForList(this.securitiesTradingParamsService.getSecuritiesTradingParamDTO()));

            }
        }

我想要一个单独的函数,它将返回所有缓存名称和缓存中数据总数的计数,以便我可以在 UI 中显示包含数据的缓存。我搜索了问题,但我没有没有找到解决方案。

最佳答案

Cache 实现Iterable<Cache.Entry<K,V>>因此您可以迭代条目以获取每个缓存的键和值,例如像这样:

for( Cache.Entry<String, GenericClassForList> entry : operatingParametersCache) { 
  String key = entry.getKey(); 
  GenericClassForList value = entry.getValue(); 
  //if counting just increasing a counter might be sufficient, 
  //otherwise use the key and value as needed 
}

由于您已经拥有对缓存的引用,因此只需将该方法应用于每个单独的缓存即可。

如果您没有方便的引用,您可以在缓存的使用别名上保留一些注册表(您提供别名,以便您可以使用这些别名从缓存管理器获取缓存)。如果您也无法做到这一点,您可能需要跟踪提供给缓存管理器的别名,例如通过用委托(delegate)包装它。

关于java - 创建缓存时如何将所有缓存名称及其数据放入List<>中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58007388/

相关文章:

jakarta-ee - Java EE : Proxy cannot be cast to Local Interface, 可能是类加载问题?

java - 将图像 url 存储在共享首选项中并在 Recyclerview 中显示它们

javascript - 如何防止在 node.js 中使用相同的 URL 进行两次调用

java - 如何实现ehcache自填充缓存作为hibernate二级缓存

java单线程慢

java - MQ - 查看消息 - 文本限制

java - Java EE 中 MDB 的管道和过滤器模式

java - 继承方法的切入点(在与类设计无关的上下文中)

java - 发布maven项目到本地目录

javascript - 禁用数据 url jpg 缓存