【问题标题】:Spring Data Cassandra and PreparedStatementCacheSpring Data Cassandra 和 PreparedStatementCache
【发布时间】:2020-01-19 02:39:56
【问题描述】:

我不明白如何使用 Spring Data Cassandra 实现非常简单的目标。

我想使用不同的参数值多次执行“INSERT”语句。我暂时没有映射域类,所以我使用Spring Data提供的CqlOperations接口。

当我只使用execute(String cql, Object... args) 时,Cassandra 驱动程序抱怨“重新准备已经准备好的查询通常是一种反模式,可能会影响性能。考虑只准备一次语句”。因为 Spring Data 使用SimplePreparedStatementCreator。但我看不出有任何方法可以告诉 Spring Data 使用CachedPreparedStatementCreator。我看到的只是execute(PreparedStatementCreator psc) 方法,它不允许我提供参数值。

那么,有没有办法告诉 Spring Data 使用正确的语句缓存或实现类似于 execute(PreparedStatementCreator, Object...) 的东西?

【问题讨论】:

    标签: java spring cassandra spring-data spring-data-cassandra


    【解决方案1】:

    CqlTemplate 公开了回调和自定义钩子,允许根据您的应用程序的需要定制其某些功能。

    CqlTemplate 确实故意没有缓存,因为缓存会导致时间与空间的考虑。 Spring Data Cassandra 无法做出决定,因为我们无法假设应用程序通常需要什么。

    Spring Data Cassandra 的包 core.cql.support 附带了对 CachedPreparedStatementCreator 的支持,以及可用于此目的的 PreparedStatementCache

    子类CqlTemplate 并覆盖其newPreparedStatementCreator(…) 方法以指定使用哪个PreparedStatementCreator。以下示例显示了具有无限保留的缓存示例:

    public class MyCachedCqlTemplate extends CqlTemplate {
    
        PreparedStatementCache cache = MapPreparedStatementCache.create();
    
        @Override
        protected PreparedStatementCreator newPreparedStatementCreator(String cql) {
            return CachedPreparedStatementCreator.of(cache, cql);
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 2019-08-19
    • 2018-10-19
    • 2018-08-23
    • 2020-10-31
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2016-02-09
    相关资源
    最近更新 更多