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(); }
public CacheItemHolder mapRow(ResultSet rs, Connection connection) throws SQLException { URI uri = URI.create(rs.getString("uri")); Vary vary = convertToVary(rs.getString("vary")); Blob blob = rs.getBlob("payload"); Payload payload = null; if (blob != null && !rs.wasNull()) { payload = new InputStreamPayload( new ResultSetInputStream(rs, connection, blob.getBinaryStream()), MIMEType.valueOf(rs.getString("mimetype"))); } Status status = Status.valueOf(rs.getInt("status")); Headers headers = convertToHeaders(rs.getString("headers")); DateTime cacheTime = new DateTime(rs.getTimestamp("cachetime").getTime()); HTTPResponse response = new HTTPResponse(payload, status, headers); return new CacheItemHolder(uri, vary, new CacheItem(rewriteResponse(response), cacheTime)); }