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