将Spring Cache与Hazelcast放在Cache附近

问题描述 投票:1回答:1

我正在尝试使用Hazelcast配置Spring CacheManager。另外,我想配置Hazelcast的Near Cache,以便可以检索已缓存对象的(已反序列化的)实例。

这是我的配置


    @Bean
    public HazelcastInstance hazelcastConfig() {
        val config = new Config().setInstanceName("instance");
        val serializationConfig = config.getSerializationConfig();
        addCacheConfig(config, "USERS")
        serializationConfig.addSerializerConfig(new SerializerConfig()
                        .setImplementation(getSerializer())
                        .setTypeClass(User.class)
        return Hazelcast.newHazelcastInstance(config);
    }

    @Bean
    public CacheManager cacheManager(HazelcastInstance hazelcastInstance) {
        return new HazelcastCacheManager(hazelcastInstance);
    }

    @Bean
    public PlatformTransactionManager chainedTransactionManager(PlatformTransactionManager jpaTransactionManager, HazelcastInstance hazelcastInstance) {
        return new ChainedTransactionManager(
                jpaTransactionManager,
                new HazelcastTransactionManager(hazelcastInstance)
        );
    }

    // Configure Near Cache
    private void addCacheConfig(Config config, String cacheName) {
        val nearCacheConfig = new NearCacheConfig()
                .setInMemoryFormat(OBJECT)
                .setCacheLocalEntries(true)
                .setInvalidateOnChange(false)
                .setTimeToLiveSeconds(hazelcastProperties.getTimeToLiveSeconds())
                .setEvictionConfig(new EvictionConfig()
                        .setMaxSizePolicy(ENTRY_COUNT)
                        .setEvictionPolicy(EvictionPolicy.LRU)
                        .setSize(hazelcastProperties.getMaxEntriesSize()));
        config.getMapConfig(cacheName)
                .setInMemoryFormat(BINARY)
                .setNearCacheConfig(nearCacheConfig);
    }

从缓存保存和检索工作正常,但是每次遇到缓存命中时,都会反序列化我的对象。我想避免使用NearCache进行反序列化,但这是行不通的。我也尝试了BINARY内存格式。

使用Hazelcast可以吗?还是即使我有NearCache也总是执行反序列化?

谢谢

java spring caching hazelcast
1个回答
0
投票

因此,进行了一些更改后,它现在可以正常工作。这是我的结论:

因此,为了使NearCache与Spring Cache一起使用,所有缓存的对象都应为Immutable。这意味着最终课程和最终领域。同样,它们都扩展了Serializable接口。

© www.soinside.com 2019 - 2024. All rights reserved.