Infinispan - 创建自定义缓存存储

时间:2015-05-06 11:07:04

标签: cassandra infinispan

由于Infinispan不再维护正式的cassandra缓存存储,我正试图制作一个我自己的(针对Infinispan 7.1.1)。

我不确定我应该实现什么,因为我在创建自定义缓存存储的文档中没有找到任何内容。

这些是我的类(受最新版本的infinispan-cachestore-mongodb启发 - 自Infinispan 6.0.0以来也弃用了:D):

public class CassandraCache<K, V> {

    private Cluster cluster;
    private Session session;
    ...
}
public class CassandraEntry<K, V>
public class CassandraStore<K, V> implements AdvancedLoadWriteStore<K, V> {
    private InitializationContext context;
    private CassandraCache<K, V> cache;
    private CassandraStoreConfiguration configuration;

    @Override
    public void init(InitializationContext ctx) {
        context = ctx;
        configuration = ctx.getConfiguration();
        try {
            cache = new CassandraCache<K, V>(configuration);
        } catch (Exception e) {
            throw new PersistenceException(e);
        }
    }
    ...
}
public class CassandraStoreConfiguration extends AbstractStoreConfiguration {

  private String hosts;
  private String keyspace;
...
}
public class CassandraStoreConfigurationBuilder extends AbstractStoreConfigurationBuilder<CassandraStoreConfiguration, CassandraStoreConfigurationBuilder> {

  private String hosts;
  private String keyspace;
...
}

我正在尝试将其设置为(在scala中):

object Infinispan {

  val m: EmbeddedCacheManager = new DefaultCacheManager(globalConfig, cacheConfig)  

  def globalConfig = {
    new GlobalConfigurationBuilder()
      .transport()
    .defaultTransport()
      .build()
  }

  def cacheConfig = {
    new ConfigurationBuilder()
    .persistence().addStore(classOf[CassandraStoreConfigurationBuilder])
      .fetchPersistentState(true)
      .preload(true)
      .shared(true)
      .hosts("localhost:9160")
      .keyspace("mykeyspace")
    .transaction()
      .transactionMode(TransactionMode.TRANSACTIONAL)
      .transactionManagerLookup(new GenericTransactionManagerLookup)
      .autoCommit(false).transactionProtocol(TransactionProtocol.DEFAULT)
      .lockingMode(LockingMode.PESSIMISTIC)
    .build()
  }

}

但我得到一个例外:

 java.lang.ClassCastException: org.infinispan.persistence.cassandra.configuration.CassandraStoreConfiguration cannot be cast to org.infinispan.configuration.cache.CustomStoreConfiguration

所以我改变了CassandraStoreConfiguration以扩展CustomStoreConfiguration和CassandraStoreConfigurationBuilder以扩展CustomStoreConfigurationBuilder

现在我得到了这个例外:

 java.lang.ClassCastException: org.infinispan.configuration.cache.CustomStoreConfiguration cannot be cast to org.infinispan.persistence.cassandra.configuration.CassandraStoreConfiguration

为什么会尝试将CustomStoreConfiguration转换为CassandraStoreConfiguration?

是否有适当的指南在某处创建自定义缓存存储?

2 个答案:

答案 0 :(得分:4)

我坚信我们全新的Infinispan自定义缓存商店原型可以真正帮助您完成工作。

请查看:https://github.com/infinispan/infinispan-cachestore-archetype

README文件包含如何使用它的必要信息。

答案 1 :(得分:3)

我只是遗漏了几个注释:

@BuiltBy(CassandraStoreConfigurationBuilder.class)
@ConfigurationFor(CassandraStore.class)
public class CassandraStoreConfiguration extends AbstractStoreConfiguration
@ConfiguredBy(CassandraStoreConfiguration.class)
public class CassandraStore<K, V> implements AdvancedLoadWriteStore<K, V>

一旦添加了这些,一切都开始正常工作。