【发布时间】:2016-04-09 07:57:22
【问题描述】:
我相信我在 Cassandra csharp 驱动程序(版本 2.7.3)的 StatementFactory 中缓存准备语句的逻辑上发现了一个错误。这是用例。
Guid key = Guid.NewGuid(); // your key
ISession session_foo = new Session("foo"); //This is pseudo code
ISession session_bar = new Session("bar");
var foo_mapper = new Mapper(session_foo); //table foo_bar
var bar_mapper = new Mapper(session_bar); //table foo_bar
await Task.WhenAll(
foo_mapper.DeleteAsync<Foo>("WHERE id = ?", key),
bar_mapper.DeleteAsync<Bar>("WHERE id = ?", key));
我们发现运行这个删除后,只有第一个请求成功。深入了解StatementFactory的源代码
public Task<Statement> GetStatementAsync(ISession session, Cql cql)
{
if (cql.QueryOptions.NoPrepare)
{
// Use a SimpleStatement if we're not supposed to prepare
Statement statement = new SimpleStatement(cql.Statement, cql.Arguments);
SetStatementProperties(statement, cql);
return TaskHelper.ToTask(statement);
}
return _statementCache
.GetOrAdd(cql.Statement, session.PrepareAsync)
.Continue(t =>
{
if (_statementCache.Count > MaxPreparedStatementsThreshold)
{
Logger.Warning(String.Format("The prepared statement cache contains {0} queries. Use parameter markers for queries. You can configure this warning threshold using MappingConfiguration.SetMaxStatementPreparedThreshold() method.", _statementCache.Count));
}
Statement boundStatement = t.Result.Bind(cql.Arguments);
SetStatementProperties(boundStatement, cql);
return boundStatement;
});
}
可以看到缓存只使用了cql语句。在我们的例子中,我们在不同的键空间(又名会话)中有相同的表名。我们在两个查询中的 cql 语句看起来是一样的。即 DELETE FROM foo_bar WHERE id =?.
如果我不得不猜测,我会说一个简单的解决方法是将 cql 语句和键空间组合在一起作为缓存键。
以前有没有其他人遇到过这个问题?
【问题讨论】:
标签: c# caching cassandra datastax opscenter