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.http.impl.client.cache; 28 29 import java.io.IOException; 30 31 import org.apache.http.annotation.Contract; 32 import org.apache.http.annotation.ThreadingBehavior; 33 import org.apache.http.client.cache.HttpCacheEntry; 34 import org.apache.http.client.cache.HttpCacheStorage; 35 import org.apache.http.client.cache.HttpCacheUpdateCallback; 36 37 /** 38 * Basic {@link HttpCacheStorage} implementation backed by an instance of 39 * {@link java.util.LinkedHashMap}. In other words, cache entries and 40 * the cached response bodies are held in-memory. This cache does NOT 41 * deallocate resources associated with the cache entries; it is intended 42 * for use with {@link HeapResource} and similar. This is the default cache 43 * storage backend used by {@link CachingHttpClients}. 44 * 45 * @since 4.1 46 */ 47 @Contract(threading = ThreadingBehavior.SAFE) 48 public class BasicHttpCacheStorage implements HttpCacheStorage { 49 50 private final CacheMap entries; 51 52 public BasicHttpCacheStorage(final CacheConfig config) { 53 super(); 54 this.entries = new CacheMap(config.getMaxCacheEntries()); 55 } 56 57 /** 58 * Places a HttpCacheEntry in the cache 59 * 60 * @param url 61 * Url to use as the cache key 62 * @param entry 63 * HttpCacheEntry to place in the cache 64 */ 65 @Override 66 public synchronized void putEntry(final String url, final HttpCacheEntry entry) throws IOException { 67 entries.put(url, entry); 68 } 69 70 /** 71 * Gets an entry from the cache, if it exists 72 * 73 * @param url 74 * Url that is the cache key 75 * @return HttpCacheEntry if one exists, or null for cache miss 76 */ 77 @Override 78 public synchronized HttpCacheEntry getEntry(final String url) throws IOException { 79 return entries.get(url); 80 } 81 82 /** 83 * Removes a HttpCacheEntry from the cache 84 * 85 * @param url 86 * Url that is the cache key 87 */ 88 @Override 89 public synchronized void removeEntry(final String url) throws IOException { 90 entries.remove(url); 91 } 92 93 @Override 94 public synchronized void updateEntry( 95 final String url, 96 final HttpCacheUpdateCallback callback) throws IOException { 97 final HttpCacheEntry existingEntry = entries.get(url); 98 entries.put(url, callback.update(existingEntry)); 99 } 100 101 }