gpt4 book ai didi

java - 使用 spring 配置 Apache Ignite mongo

转载 作者:行者123 更新时间:2023-12-01 21:20:20 25 4
gpt4 key购买 nike

我在我们的应用程序中引入 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 的文档。

这个想法不是直接与数据库对话,而是通过使用 write Behind 功能的 ignite 进行对话。

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

按照建议我实现了

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)

最佳答案

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

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

关于java - 使用 spring 配置 Apache Ignite mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com