You can create a cache (this is a fairly simple example to give you an idea) of the statements you need. Let's start by creating a class that will be used as a cache.
private class StatementCache { Map<String, PreparedStatement> statementCache = new HashMap<>(); public BoundStatement getStatement(String cql) { PreparedStatement ps = statementCache.get(cql);
Then add the instance to your singleton:
public class TestCassandra { private Session session = null; private Cluster cluster = null; private StatementCache psCache = new StatementCache();
And finally, use the cache from your function:
private Set<String> getRandomUsers(PreparedStatement ps) { // lots of code. try { SimpleStatement query = new SimpleStatement(sql); query.setConsistencyLevel(ConsistencyLevel.QUORUM); // abstract the handling of the cache to it own class. // this will need some work to make sure it thread safe // as currently it not. ResultSet res = session.execute(psCache.getStatement(sql));
source share