【问题标题】:Apache Ignite mongo configuration using spring使用spring的Apache Ignite mongo配置
【发布时间】:2016-08-29 11:04:39
【问题描述】:

我在我们的应用程序中引入 Apache Ignite 作为缓存系统以及计算。我已经使用以下配置类配置了 spring 应用程序。

@Configuration
@EnableCaching
public class IgniteConfig {

    @Value("${ignite.config.path}")
    private String ignitePath;

    @Bean(name="cacheManager")
    public SpringCacheManager cacheManager(){
        SpringCacheManager springCacheManager = new SpringCacheManager();
        springCacheManager.setConfigurationPath(ignitePath);
        return springCacheManager;
    }
}

像这样使用它

@Override
@Cacheable("cache1")
public List<Channel>  getAllChannels(){
    List<Channel> list = new ArrayList<Channel>();
    Channel c1 = new Channel("1",1);
    Channel c2 = new Channel("2",2);
    Channel c3 = new Channel("3",3);
    Channel c4 = new Channel("4",4);
    list.add(c1);
    list.add(c2);
    list.add(c3);
    list.add(c4);
    return list;
}

现在我想添加直写和直读功能。我找不到任何将 ignite 连接到 mongo 的文档。

这个想法不是直接与 db 对话,而是通过使用 write behind 功能来点燃。

编辑-----------

按照建议我实施了

public class ChannelCacheStore extends CacheStoreAdapter<Long, Channel> implements Serializable {

    @Override
    public Channel load(Long key) throws CacheLoaderException {
        return getChannelDao().findOne(Channel.mongoChannelCode, key);
    }

    @Override
    public void write(Cache.Entry<? extends Long, ? extends Channel> entry) throws CacheWriterException {
        getChannelDao().save(entry.getValue());
    }

    @Override
    public void delete(Object key) throws CacheWriterException {
        throw new UnsupportedOperationException("Delete not supported");
    }

    private ChannelDao getChannelDao(){
        return SpringContextUtil.getApplicationContext().getBean(ChannelDao.class);
    }
}

并将这个 CacheStore 添加到缓存配置中,如下所示:

<property name="cacheConfiguration">
        <list>
            <bean class="org.apache.ignite.configuration.CacheConfiguration">
                <property name="name" value="channelCache"/>
                <property name="cacheMode" value="PARTITIONED"/>
                <property name="atomicityMode" value="ATOMIC"/>
                <property name="backups" value="1"/>
                <property name="readThrough" value="true"/>
                <!-- Sets flag indicating whether write to database is enabled. -->
                <property name="writeThrough" value="true"/>
                <!-- Enable database batching. --> 
                <!-- Sets flag indicating whether write-behind is enabled. -->
                <property name="writeBehindEnabled" value="true"/>
                <property name="cacheStoreFactory">
                    <bean class="javax.cache.configuration.FactoryBuilder$SingletonFactory">
                        <constructor-arg>
                            <bean class="in.per.amt.ignite.cache.ChannelCacheStore"></bean>
                        </constructor-arg>
                    </bean>
                </property>
            </bean>
        </list>
    </property>

但是现在得到类转换异常

java.lang.ClassCastException: org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.Long
    at in.per.amt.ignite.cache.ChannelCacheStore.load(ChannelCacheStore.java:19)

【问题讨论】:

    标签: java spring spring-mvc ignite


    【解决方案1】:

    您可以通过实现CacheStore 接口来拥有任何类型的后备数据库:

    https://apacheignite.readme.io/docs/persistent-store

    【讨论】:

      【解决方案2】:

      您是否尝试过设置密钥生成器?

      @CacheConfig(cacheNames = "cache1",keyGenerator = "simpleKeyGenerator")

      https://github.com/spring-projects/spring-boot/issues/3625

      【讨论】:

        【解决方案3】:

        所以在下面你分享的代码行中,

        @Cacheable("cache1")
        public List<Channel>  getAllChannels(){
        

        @Cacheable 注解用于不接受任何参数的方法。 Spring 缓存使用参数(如果是基本数据类型)作为缓存的键(响应 obj 作为值)。我相信这会使缓存无效。

        【讨论】:

        • 我认为这完全取决于您使用的缓存键
        猜你喜欢
        • 2021-04-22
        • 2023-02-21
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2018-03-03
        • 2018-12-17
        • 1970-01-01
        • 2016-02-01
        相关资源
        最近更新 更多