View Javadoc
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.HttpAsyncCacheStorage;
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.ResourceIOException;
37  import org.apache.hc.client5.http.impl.Operations;
38  import org.apache.hc.core5.concurrent.Cancellable;
39  import org.apache.hc.core5.concurrent.FutureCallback;
40  
41  class SimpleHttpAsyncCacheStorage implements HttpAsyncCacheStorage {
42  
43      public final Map<String,HttpCacheEntry> map;
44  
45      public SimpleHttpAsyncCacheStorage() {
46          map = new HashMap<>();
47      }
48  
49      @Override
50      public Cancellable putEntry(final String key, final HttpCacheEntry entry, final FutureCallback<Boolean> callback) {
51          map.put(key, entry);
52          if (callback != null) {
53              callback.completed(true);
54          }
55          return Operations.nonCancellable();
56      }
57  
58      public void putEntry(final String key, final HttpCacheEntry entry) {
59          map.put(key, entry);
60      }
61  
62      @Override
63      public Cancellable getEntry(final String key, final FutureCallback<HttpCacheEntry> callback) {
64          final HttpCacheEntry entry = map.get(key);
65          if (callback != null) {
66              callback.completed(entry);
67          }
68          return Operations.nonCancellable();
69      }
70  
71      public HttpCacheEntry getEntry(final String key) {
72          return map.get(key);
73      }
74  
75      @Override
76      public Cancellable removeEntry(final String key, final FutureCallback<Boolean> callback) {
77          final HttpCacheEntry removed = map.remove(key);
78          if (callback != null) {
79              callback.completed(removed != null);
80          }
81          return Operations.nonCancellable();
82      }
83  
84      @Override
85      public Cancellable updateEntry(final String key, final HttpCacheCASOperation casOperation, final FutureCallback<Boolean> callback) {
86          final HttpCacheEntry v1 = map.get(key);
87          try {
88              final HttpCacheEntry v2 = casOperation.execute(v1);
89              map.put(key,v2);
90              if (callback != null) {
91                  callback.completed(true);
92              }
93          } catch (final ResourceIOException ex) {
94              if (callback != null) {
95                  callback.failed(ex);
96              }
97          }
98          return Operations.nonCancellable();
99      }
100 
101     @Override
102     public Cancellable getEntries(final Collection<String> keys, final FutureCallback<Map<String, HttpCacheEntry>> callback) {
103         final Map<String, HttpCacheEntry> resultMap = new HashMap<>(keys.size());
104         for (final String key: keys) {
105             final HttpCacheEntry entry = map.get(key);
106             if (entry != null) {
107                 resultMap.put(key, entry);
108             }
109         }
110         callback.completed(resultMap);
111         return Operations.nonCancellable();
112     }
113 
114 }