1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 package org.apache.hc.client5.http.impl.cache; 28 29 import java.time.Instant; 30 import java.util.Map; 31 32 import org.apache.hc.client5.http.cache.HttpCacheEntry; 33 import org.apache.hc.core5.http.HttpHost; 34 import org.apache.hc.core5.http.HttpRequest; 35 import org.apache.hc.core5.http.HttpResponse; 36 import org.apache.hc.core5.util.ByteArrayBuffer; 37 38 interface HttpCache { 39 40 String generateKey (HttpHost host, HttpRequest request, HttpCacheEntry cacheEntry); 41 42 /** 43 * Clear all matching {@link HttpCacheEntry}s. 44 */ 45 void flushCacheEntriesFor(HttpHost host, HttpRequest request); 46 47 /** 48 * Flush {@link HttpCacheEntry}s invalidated by the given request 49 */ 50 void flushCacheEntriesInvalidatedByRequest(HttpHost host, HttpRequest request); 51 52 /** 53 * Flush {@link HttpCacheEntry}s invalidated by the given message exchange. 54 */ 55 void flushCacheEntriesInvalidatedByExchange(HttpHost host, HttpRequest request, HttpResponse response); 56 57 /** 58 * Retrieve matching {@link HttpCacheEntry} from the cache if it exists. 59 */ 60 HttpCacheEntry getCacheEntry(HttpHost host, HttpRequest request); 61 62 /** 63 * Retrieve all variants from the cache, if there are no variants then an empty 64 * {@link Map} is returned 65 */ 66 Map<String,Variant> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest request); 67 68 /** 69 * Store a {@link HttpResponse} in the cache if possible, and return 70 */ 71 HttpCacheEntry createCacheEntry( 72 HttpHost host, 73 HttpRequest request, 74 HttpResponse originResponse, 75 ByteArrayBuffer content, 76 Instant requestSent, 77 Instant responseReceived); 78 79 /** 80 * Update a {@link HttpCacheEntry} using a 304 {@link HttpResponse}. 81 */ 82 HttpCacheEntry updateCacheEntry( 83 HttpHost host, 84 HttpRequest request, 85 HttpCacheEntry stale, 86 HttpResponse originResponse, 87 Instant requestSent, 88 Instant responseReceived); 89 90 /** 91 * Update a specific {@link HttpCacheEntry} representing a cached variant 92 * using a 304 {@link HttpResponse}. 93 */ 94 HttpCacheEntry updateVariantCacheEntry( 95 HttpHost host, 96 HttpRequest request, 97 HttpResponse originResponse, 98 Variant variant, 99 Instant requestSent, 100 Instant responseReceived); 101 102 /** 103 * Specifies cache should reuse the given cached variant to satisfy 104 * requests whose varying headers match those of the given client request. 105 */ 106 void reuseVariantEntryFor( 107 HttpHost host, 108 HttpRequest request, 109 Variant variant); 110 }