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