1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.api.cache;
20
21 import java.util.List;
22 import java.util.function.Function;
23
24 import org.apache.maven.api.annotations.Experimental;
25 import org.apache.maven.api.services.Request;
26 import org.apache.maven.api.services.Result;
27
28 /**
29 * Interface for caching request results in Maven. This cache implementation provides
30 * methods for executing and optionally caching both single requests and batches of requests.
31 * <p>
32 * The cache behavior is determined by the cache retention specified in the request's metadata.
33 * Results can be cached at different policies (forever, session, request, or not at all)
34 * based on the {@link CacheRetention} associated with the request.
35 *
36 * @since 4.0.0
37 * @see CacheMetadata
38 * @see RequestCacheFactory
39 */
40 @Experimental
41 public interface RequestCache {
42
43 /**
44 * Executes and optionally caches a request using the provided supplier function. If caching is enabled
45 * for this session, the result will be cached and subsequent identical requests will return the cached
46 * value without re-executing the supplier.
47 * <p>
48 * The caching behavior is determined by the cache retention specified in the request's metadata.
49 * If an error occurs during execution, it will be cached and re-thrown for subsequent identical requests.
50 *
51 * @param <REQ> The request type
52 * @param <REP> The response type
53 * @param req The request object used as the cache key
54 * @param supplier The function to execute and cache the result
55 * @return The result from the supplier (either fresh or cached)
56 * @throws RuntimeException Any exception thrown by the supplier will be cached and re-thrown on subsequent calls
57 */
58 <REQ extends Request<?>, REP extends Result<REQ>> REP request(REQ req, Function<REQ, REP> supplier);
59
60 /**
61 * Executes and optionally caches a batch of requests using the provided supplier function.
62 * This method allows for efficient batch processing of multiple requests.
63 * <p>
64 * The implementation may optimize the execution by:
65 * <ul>
66 * <li>Returning cached results for previously executed requests</li>
67 * <li>Grouping similar requests for batch processing</li>
68 * <li>Processing requests in parallel where appropriate</li>
69 * </ul>
70 *
71 * @param <REQ> The request type
72 * @param <REP> The response type
73 * @param req List of requests to process
74 * @param supplier Function to execute the batch of requests
75 * @return List of results corresponding to the input requests
76 * @throws BatchRequestException if any request in the batch fails
77 */
78 <REQ extends Request<?>, REP extends Result<REQ>> List<REP> requests(
79 List<REQ> req, Function<List<REQ>, List<REP>> supplier);
80 }