/// <summary> /// Creates and caches a prepared statement. It can be used to optimise queries execution time /// </summary> /// <param name="builder">The builder which builds the query</param> /// <param name=""></param> /// <returns></returns> public PreparedStatement GetPreparedStatement(string key, CassandraQueryBuilder builder = null) { if (this.CachedStatements.ContainsKey(key)) { return(this.CachedStatements[key]); } if (builder == null) { return(null); } if (this.currentSession == null) { createConnection(); } try { Func <PreparedStatement> waitingFunction = () => { var task = this.statementCreationTasks[key]; task.Wait(); return(this.CachedStatements[key]); }; if (this.statementCreationTasks.ContainsKey(key)) { return(waitingFunction()); } Task creationTask = null; lock (creatingTask) { if (this.statementCreationTasks.ContainsKey(key)) { return(waitingFunction()); } creationTask = Task.Run(async() => { PreparedStatement statement = await this.currentSession.PrepareAsync(builder.Build()); this.CachedStatements.AddOrUpdate(key, statement, (k, v) => v); }); this.statementCreationTasks.TryAdd(key, creationTask); } creationTask.Wait(); return(this.CachedStatements[key]); } catch { return(null); } }
public void PrepareQuery(CassandraQueryBuilder builder) { this.query = builder.Build(); }