简体   繁体   English

InfiniSpan如何获取缓存

[英]InfiniSpan how to obtain cache

I'm trying to obtain the InfiniSpan cache with the name 'default' (defined in my persistence.xml) via the Hibernate Session:我正在尝试通过 Hibernate Session 获取名称为“default”的 InfiniSpan 缓存(在我的 persistence.xml 中定义):

Code to obtain cache #1:获取缓存 #1 的代码:

            EntityManagerFactory emf = Persistence.createEntityManagerFactory("mytest");
            SessionFactory unwrap = emf.unwrap(SessionFactory.class);
            CacheImplementor cache = (CacheImplementor) unwrap.getCache();
            RegionFactory regionFactory = cache.getRegionFactory();

            if (regionFactory instanceof InfinispanRegionFactory) {
                InfinispanRegionFactory reg = (InfinispanRegionFactory) regionFactory;
                EmbeddedCacheManager cacheManager = reg.getCacheManager();
            }


            final Cache<String, String> cache2 = cacheManager.getCache("default");

            AdvancedCache ac = cache2.getAdvancedCache();

            TransactionManager tm = cache2.getAdvancedCache().getTransactionManager();

Code to obtain cache #2:获取缓存 #2 的代码:

            // Not using EntityManagerFactory here, because otherwise the test-infinispan.xml is already loaded.
            EmbeddedCacheManager cacheManager = new DefaultCacheManager("./resources/test-infinispan.xml");

            final Cache<String, String> cache = cacheManager.getCache("default");
            TransactionManager tm = cache.getAdvancedCache().getTransactionManager();

Code that I use after the tm has been initialized:我在 tm 初始化后使用的代码:


            try {
                tm.begin();
                
                if (cache.get("test1") != null) { 
                    System.out.println("FOUND IN CACHE: test1"); 
                } 

                cache.put("test1", "test1");

                tm.commit();

            } catch (Exception e) {
                try {
                    tm.rollback();
                } catch (Exception e2) {
                }
            } finally {
                cacheManager.stop();
            }

With #2 I can obtain the "default" cache and add items to it.使用#2,我可以获得“默认”缓存并向其中添加项目。 The transaction manager tm is not null, so that's all fine.事务管理器 tm 不是 null,所以没关系。 After a restart the data saved in the cache is available again.重新启动后,保存在缓存中的数据再次可用。

With #1 the transaction manager tm is null.对于#1,事务管理器 tm 是 null。 Using another instance of TransactionManager I can add items to the cache, but they are not available anymore after a restart.使用 TransactionManager 的另一个实例,我可以将项目添加到缓存中,但重新启动后它们不再可用。 I also tried working with @Cacheable region.我还尝试使用@Cacheable 区域。

Properties used in persistence.xml: persistence.xml 中使用的属性:

            <property name="hibernate.ogm.infinispan.configuration_resource_name" value="test-infinispan.xml" />
            <property name="hibernate.search.default.directory_provider" value="infinispan" />
            <property name="hibernate.search.infinispan.configuration_resourcename" value="test-infinispan-index.xml" />

            <property name="hibernate.cache.use_second_level_cache" value="true"></property>
            <property name="hibernate.cache.use_query_cache" value="true"></property>
            <property name="hibernate.cache.region.factory_class" value="org.infinispan.hibernate.cache.v53.InfinispanRegionFactory"></property>
            <property name="hibernate.cache.inifinispan.statistics" value="true"></property>
            <property name="hibernate.cache.default_cache_concurrency_strategy" value="transactional"></property>

            <property name="hibernate.search.services.jgroups.configurationFile" value="test-jgroups-tcp-static-cluster.xml" />
            <property name="hibernate.search.services.jgroups.clusterName" value="default-test-index-cluster-2" />

The EntityManagerFactory loads the persistence-unit "mytest" and initializes the whole structure starting from persistence.xml, the InfiniSpan config., the InfiniSpan index config. EntityManagerFactory 加载持久化单元“mytest”并从persistence.xml、InfiniSpan 配置、InfiniSpan 索引配置开始初始化整个结构。 and the jgroups config.和 jgroups 配置。

test-infinispan.xml:测试-infinispan.xml:

    <cache-container name="Database-Container" default-cache="default" statistics="false" shutdown-hook="DONT_REGISTER">
<transport stack="default-tcp-static-cluster" cluster="default-test-cluster" /> 
        <replicated-cache name="default" mode="SYNC" remote-timeout="480000">
            <locking striping="false" acquire-timeout="120000" concurrency-level="500" isolation="REPEATABLE_READ" />
            
            <transaction locking="PESSIMISTIC" auto-commit="false" mode="FULL_XA" transaction-manager-lookup="org.infinispan.transaction.lookup.JBossStandaloneJTAManagerLookup"/>
           <indexing index="NONE"></indexing>
            <memory>
                <object size="-1" />
            </memory>
            <expiration max-idle="-1" />

            <state-transfer enabled="true" timeout="480000" await-initial-transfer="true" />
            <persistence  passivation="false">
                <file-store shared="false" preload="false" fetch-state="true" read-only="false" purge="false" path="./database_data/DEFAULT">
                    <write-behind modification-queue-size="5" fail-silently="false"/>
                </file-store>
            </persistence>
        </replicated-cache> 
</cache-container>

What do I need in order to obtain the "default" cache in example #1?为了获得示例#1 中的“默认”缓存,我需要什么? Or is that not possible?或者那不可能? Or do I need to work with regions (using the @Cacheable annotation) and if so, what do I need in order to make the cache (ephemeral?) persistent, so I can reload the data after a restart.或者我是否需要使用区域(使用@Cacheable 注释),如果需要,我需要什么才能使缓存(临时?)持久化,以便我可以在重新启动后重新加载数据。

I stick with using this way to get a cache-manager:我坚持使用这种方式来获取缓存管理器:

EmbeddedCacheManager cacheManager = new DefaultCacheManager("my-cache.xml"); EmbeddedCacheManager cacheManager = new DefaultCacheManager("my-cache.xml");

with a cache specified in a separate xml file.在单独的 xml 文件中指定缓存。 This looks like what I need.这看起来像我需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM