DATACASS-814 - Use CQL in MapPreparedStatementCache.CacheKey instead representation of Object.toString().
We now correctly use the CQL text to cache prepared statements. Previously, we used toString() which was a leftover from the driver migration that now defaults to Object.toString() and so the cache key was not stable. Original pull request: #180.
This commit is contained in:
@@ -34,6 +34,7 @@ import com.datastax.oss.driver.api.core.cql.SimpleStatement;
|
||||
* {@code cql} text. Statement options (idempotency, timeouts) apply from the statement that was initially prepared.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Aldo Bongio
|
||||
* @since 2.0
|
||||
*/
|
||||
public class MapPreparedStatementCache implements PreparedStatementCache {
|
||||
@@ -84,7 +85,7 @@ public class MapPreparedStatementCache implements PreparedStatementCache {
|
||||
public PreparedStatement getPreparedStatement(CqlSession session, SimpleStatement statement,
|
||||
Supplier<PreparedStatement> preparer) {
|
||||
|
||||
CacheKey cacheKey = new CacheKey(session, statement.toString());
|
||||
CacheKey cacheKey = new CacheKey(session, statement.getQuery());
|
||||
|
||||
return getCache().computeIfAbsent(cacheKey, key -> preparer.get());
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.datastax.oss.driver.api.querybuilder.update.Assignment;
|
||||
* Unit tests for {@link CachedPreparedStatementCreator}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Aldo Bongio
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
@@ -182,4 +183,17 @@ class CachedPreparedStatementCreatorUnitTests {
|
||||
verify(session).prepare(firstStatement);
|
||||
verify(session).prepare(secondStatement);
|
||||
}
|
||||
|
||||
@Test // DATACASS-814
|
||||
void shouldUseCqlTextInCacheKey() {
|
||||
|
||||
String cql = "SELECT foo FROM users;";
|
||||
|
||||
MapPreparedStatementCache cache = MapPreparedStatementCache.create();
|
||||
CachedPreparedStatementCreator creator = CachedPreparedStatementCreator.of(cache, cql);
|
||||
creator.createPreparedStatement(session);
|
||||
|
||||
MapPreparedStatementCache.CacheKey cacheKey = cache.getCache().keySet().iterator().next();
|
||||
assertThat(cacheKey.cql).isSameAs(cql);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user