【问题标题】:Using Spring Cache with Hazelcast Near Cache使用 Spring Cache 和 Hazelcast Near Cache
【发布时间】:2020-02-19 23:01:17
【问题描述】:

我正在尝试使用 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】:

    所以经过一些更改后,它现在可以工作了。这是我的结论:

    所以为了让 NearCache 与 Spring Cache 一起工作,所有缓存的对象都应该是 Immutable。这意味着最终类和最终字段。而且,它们都扩展了他的 Serializable 接口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多