Skip to content

Commit 0208810

Browse files
Check cache before executing a request
1 parent 68e96fb commit 0208810

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/main/java/com/qwazr/jdbc/cache/CachedPreparedStatement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ public void setObject(int parameterIndex, Object x) throws SQLException {
206206

207207
@Override
208208
public boolean execute() throws SQLException {
209+
if (connection.resultSetCache.checkIfExists(ResultSetCache.getKey(executedSql, parameters)))
210+
return true;
209211
if (backendStatement != null)
210212
return backendStatement.execute();
211213
else
212-
return connection.resultSetCache.checkExists(ResultSetCache.getKey(executedSql, parameters));
214+
throw new SQLException("No cache entry");
213215
}
214216

215217
@Override

src/main/java/com/qwazr/jdbc/cache/CachedStatement.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ public void setCursorName(String name) throws SQLException {
162162
@Override
163163
public boolean execute(String sql) throws SQLException {
164164
this.executedSql = sql;
165+
if (connection.resultSetCache.checkIfExists(ResultSetCache.getKey(executedSql)))
166+
return true;
165167
if (backendStatement != null)
166168
return backendStatement.execute(sql);
167169
else
168-
return connection.resultSetCache.checkExists(ResultSetCache.getKey(executedSql));
170+
throw new SQLException("No cache entry");
169171
}
170172

171173
@Override
@@ -306,28 +308,34 @@ public int executeUpdate(String sql, String[] columnNames) throws SQLException {
306308
@Override
307309
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
308310
executedSql = sql;
311+
if (connection.resultSetCache.checkIfExists(ResultSetCache.getKey(executedSql)))
312+
return true;
309313
if (backendStatement != null)
310314
return backendStatement.execute(sql, autoGeneratedKeys);
311315
else
312-
return connection.resultSetCache.checkExists(ResultSetCache.getKey(executedSql));
316+
throw new SQLException("No cache entry");
313317
}
314318

315319
@Override
316320
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
317321
executedSql = sql;
322+
if (connection.resultSetCache.checkIfExists(ResultSetCache.getKey(executedSql)))
323+
return true;
318324
if (backendStatement != null)
319325
return backendStatement.execute(sql, columnIndexes);
320326
else
321-
return connection.resultSetCache.checkExists(ResultSetCache.getKey(executedSql));
327+
throw new SQLException("No cache entry");
322328
}
323329

324330
@Override
325331
public boolean execute(String sql, String[] columnNames) throws SQLException {
326332
executedSql = sql;
333+
if (connection.resultSetCache.checkIfExists(ResultSetCache.getKey(executedSql)))
334+
return true;
327335
if (backendStatement != null)
328336
return backendStatement.execute(sql, columnNames);
329337
else
330-
return connection.resultSetCache.checkExists(ResultSetCache.getKey(executedSql));
338+
throw new SQLException("No cache entry");
331339
}
332340

333341
@Override

src/main/java/com/qwazr/jdbc/cache/ResultSetCache.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,14 @@ final CachedResultSet get(final CachedStatement statement, final String key, fin
6868
}
6969

7070
/**
71-
* Check if an entry is available for this key. If not, an SQLException is thrown.
71+
* Check if an entry is available for this key.
7272
*
7373
* @param key the computed key
74-
* @return always true
75-
* @throws SQLException
74+
* @return always true if the cache entry exists
7675
*/
77-
final boolean checkExists(final String key) throws SQLException {
76+
final boolean checkIfExists(final String key) {
7877
final Path resultSetPath = cacheDirectory.resolve(key);
79-
if (!Files.exists(resultSetPath))
80-
throw new SQLException("No cache available");
81-
return true;
78+
return Files.exists(resultSetPath);
8279
}
8380

8481
interface Provider {

0 commit comments

Comments
 (0)