OkResponseCache.java revision 166772be0e5cfdaea1a64b9f63e4c8dbfe48cba3
1/*
2 * Copyright (C) 2013 Square, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.squareup.okhttp;
17
18import java.io.IOException;
19import java.net.CacheRequest;
20import java.net.CacheResponse;
21import java.net.HttpURLConnection;
22import java.net.URI;
23import java.net.URLConnection;
24import java.util.List;
25import java.util.Map;
26
27/**
28 * An extended response cache API. Unlike {@link java.net.ResponseCache}, this
29 * interface supports conditional caching and statistics.
30 *
31 * <h3>Warning: Experimental OkHttp 2.0 API</h3>
32 * This class is in beta. APIs are subject to change!
33 */
34public interface OkResponseCache {
35  CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders)
36      throws IOException;
37
38  CacheRequest put(URI uri, URLConnection urlConnection) throws IOException;
39
40  /** Remove any cache entries for the supplied {@code uri} if the request method invalidates. */
41  void maybeRemove(String requestMethod, URI uri) throws IOException;
42
43  /**
44   * Handles a conditional request hit by updating the stored cache response
45   * with the headers from {@code httpConnection}. The cached response body is
46   * not updated. If the stored response has changed since {@code
47   * conditionalCacheHit} was returned, this does nothing.
48   */
49  void update(CacheResponse conditionalCacheHit, HttpURLConnection connection) throws IOException;
50
51  /** Track an conditional GET that was satisfied by this cache. */
52  void trackConditionalCacheHit();
53
54  /** Track an HTTP response being satisfied by {@code source}. */
55  void trackResponse(ResponseSource source);
56}
57