Infinispan - 创建自定义缓存存储

Infinispan - create custom cache store

由于 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?

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

我坚信我们全新的 Infinispan 自定义缓存存储原型可以真正帮助您。

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

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

我只是漏掉了一些注释:

@BuiltBy(CassandraStoreConfigurationBuilder.class)
@ConfigurationFor(CassandraStore.class)
public class CassandraStoreConfiguration extends AbstractStoreConfiguration

@ConfiguredBy(CassandraStoreConfiguration.class)
public class CassandraStore<K, V> implements AdvancedLoadWriteStore<K, V>

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