1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.cache;
28
29 import java.util.Collection;
30 import java.util.Map;
31
32 import org.apache.hc.client5.http.impl.Operations;
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.concurrent.Cancellable;
36 import org.apache.hc.core5.concurrent.FutureCallback;
37 import org.apache.hc.core5.util.Args;
38
39
40
41
42
43
44
45 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
46 public final class HttpAsyncCacheStorageAdaptor implements HttpAsyncCacheStorage {
47
48 private final HttpCacheStorage cacheStorage;
49
50 public HttpAsyncCacheStorageAdaptor(final HttpCacheStorage cacheStorage) {
51 this.cacheStorage = Args.notNull(cacheStorage, "Cache storage");
52 }
53
54 @Override
55 public Cancellable putEntry(final String key, final HttpCacheEntry entry, final FutureCallback<Boolean> callback) {
56 Args.notEmpty(key, "Key");
57 Args.notNull(entry, "Cache entry");
58 Args.notNull(callback, "Callback");
59 try {
60 cacheStorage.putEntry(key, entry);
61 callback.completed(Boolean.TRUE);
62 } catch (final Exception ex) {
63 callback.failed(ex);
64 }
65 return Operations.nonCancellable();
66 }
67
68 @Override
69 public Cancellable getEntry(final String key, final FutureCallback<HttpCacheEntry> callback) {
70 Args.notEmpty(key, "Key");
71 Args.notNull(callback, "Callback");
72 try {
73 final HttpCacheEntry entry = cacheStorage.getEntry(key);
74 callback.completed(entry);
75 } catch (final Exception ex) {
76 callback.failed(ex);
77 }
78 return Operations.nonCancellable();
79 }
80
81 @Override
82 public Cancellable removeEntry(final String key, final FutureCallback<Boolean> callback) {
83 Args.notEmpty(key, "Key");
84 Args.notNull(callback, "Callback");
85 try {
86 cacheStorage.removeEntry(key);
87 callback.completed(Boolean.TRUE);
88 } catch (final Exception ex) {
89 callback.failed(ex);
90 }
91 return Operations.nonCancellable();
92 }
93
94 @Override
95 public Cancellable updateEntry(
96 final String key, final HttpCacheCASOperation casOperation, final FutureCallback<Boolean> callback) {
97 Args.notEmpty(key, "Key");
98 Args.notNull(casOperation, "CAS operation");
99 Args.notNull(callback, "Callback");
100 try {
101 cacheStorage.updateEntry(key, casOperation);
102 callback.completed(Boolean.TRUE);
103 } catch (final Exception ex) {
104 callback.failed(ex);
105 }
106 return Operations.nonCancellable();
107 }
108
109 @Override
110 public Cancellable getEntries(final Collection<String> keys, final FutureCallback<Map<String, HttpCacheEntry>> callback) {
111 Args.notNull(keys, "Key");
112 Args.notNull(callback, "Callback");
113 try {
114 callback.completed(cacheStorage.getEntries(keys));
115 } catch (final Exception ex) {
116 callback.failed(ex);
117 }
118 return Operations.nonCancellable();
119 }
120
121 }