java - Spring Data Cassandra 和 PreparedStatementCache
问题描述
我不明白如何使用 Spring Data Cassandra 实现非常简单的目标。
我想使用不同的参数值多次执行“INSERT”语句。我目前没有映射域类,所以我使用CqlOperations
Spring Data 提供的接口。
当我刚刚使用execute(String cql, Object... args)
时,Cassandra 驱动程序抱怨“重新准备已经准备好的查询通常是一种反模式,可能会影响性能。考虑只准备一次语句”。因为 Spring Data 使用SimplePreparedStatementCreator
. 但是我看不到任何方法可以告诉 Spring Data 使用它CachedPreparedStatementCreator
。我所看到的只是execute(PreparedStatementCreator psc)
不允许我提供参数值的方法。
那么,有没有办法告诉 Spring Data 使用正确的语句缓存或实现类似的东西execute(PreparedStatementCreator, Object...)
?
解决方案
CqlTemplate
公开回调和自定义钩子,允许根据应用程序的需要定制其某些功能。
CqlTemplate
确实故意没有缓存,因为缓存会导致时间与空间的考虑。Spring Data Cassandra 无法做出决定,因为我们无法假设应用程序通常需要什么。
Spring Data Cassandra 的包core.cql.support
附带了对CachedPreparedStatementCreator
和 a 的支持PreparedStatementCache
,您可以将其用于该目的。
子类化CqlTemplate
并覆盖其newPreparedStatementCreator(…)
方法以指定PreparedStatementCreator
使用哪个。以下示例显示了具有无限保留的缓存示例:
public class MyCachedCqlTemplate extends CqlTemplate {
PreparedStatementCache cache = MapPreparedStatementCache.create();
@Override
protected PreparedStatementCreator newPreparedStatementCreator(String cql) {
return CachedPreparedStatementCreator.of(cache, cql);
}
}
推荐阅读
- mysql - mysql服务器的空闲负载 - 如何停止它
- javascript - 页面在提交按钮上不断重新加载
- powershell - PowerShell 5.1 不工作,不允许运行任何脚本/Cmdlet
- python - 如何在字节字符串中调用变量
- r - 快速放大闪亮会使人失去控制
- windows - 使用 for 循环删除几个模式后如何从命令输出中提取动态模式
- php - 如何在 URL 中传递韩文字符
- python - Python 类型提示:`def f(e: Union[np, tf])` for `import numpy as np`?
- c# - 当一个请求处于 I/O-bound 工作状态时,多个 Web 请求是否可以使用同一个线程?
- qt - Windows 10 中内置的 Qt 程序运行速度比 Windows 7 中内置的程序慢