private HTTPResponse getImpl(Connection connection, final Key key) { PreparedStatement statement = null; try { statement = connection.prepareStatement("select * from response where uri = ? and vary = ?"); statement.setString(1, key.getURI().toString()); statement.setString(2, key.getVary().toJSON()); ResultSet rs = statement.executeQuery(); if (rs.next()) { CacheItemHolder holder = mapper.mapRow(rs, connection); return holder.getCacheItem().getResponse(); } } catch (SQLException e) { throw new DataAccessException(e); } finally { JdbcUtil.close(statement); } return null; }
@Override public int size() { Connection connection = getConnection(); PreparedStatement statement = null; ResultSet rs = null; try { statement = connection.prepareStatement("select count(*) from response"); rs = statement.executeQuery(); if (rs.next()) { return rs.getInt(1); } } catch (SQLException e) { throw new DataAccessException("Unable to query for count", e); } finally { JdbcUtil.close(rs); JdbcUtil.close(statement); JdbcUtil.close(connection); } return 0; }
@Override public CacheItem get(HTTPRequest request) { Connection connection = getConnection(); PreparedStatement statement = null; try { statement = connection.prepareStatement("select * from response where uri = ?"); statement.setString(1, request.getRequestURI().toString()); ResultSet rs = statement.executeQuery(); while (rs.next()) { CacheItemHolder holder = mapper.mapRow(rs, connection); if (holder.getVary().matches(request)) { return holder.getCacheItem(); } } } catch (SQLException e) { throw new DataAccessException(e); } finally { JdbcUtil.close(statement); } return null; }
@Override public Iterator<Key> iterator() { Connection connection = getConnection(); PreparedStatement statement = null; ResultSet rs = null; List<Key> keys = new ArrayList<Key>(); try { statement = connection.prepareStatement("select uri,vary from response"); rs = statement.executeQuery(); while (rs.next()) { String uri = rs.getString(1); String vary = rs.getString(2); keys.add(Key.create(URI.create(uri), mapper.convertToVary(vary))); } } catch (SQLException ignore) { } finally { JdbcUtil.close(rs); JdbcUtil.close(statement); JdbcUtil.close(connection); } return ImmutableList.copyOf(keys).iterator(); }