001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether;
020
021import java.util.function.Supplier;
022
023/**
024 * Caches auxiliary data used during repository access like already processed metadata. The data in the cache is meant
025 * for exclusive consumption by the repository system and is opaque to the cache implementation. <strong>Note:</strong>
026 * Actual cache implementations must be thread-safe.
027 *
028 * @see RepositorySystemSession#getCache()
029 * @noimplement This interface is not intended to be implemented by clients.
030 * @noextend This interface is not intended to be extended by clients.
031 */
032public interface RepositoryCache {
033
034    /**
035     * Puts the specified data into the cache. It is entirely up to the cache implementation how long this data will be
036     * kept before being purged, i.e. callers must not make any assumptions about the lifetime of cached data.
037     * <p>
038     * <em>Warning:</em> The cache will directly save the provided reference. If the cached data is mutable, i.e. could
039     * be modified after being put into the cache, the caller is responsible for creating a copy of the original data
040     * and store the copy in the cache.
041     *
042     * @param session The repository session during which the cache is accessed, must not be {@code null}.
043     * @param key The key to use for lookup of the data, must not be {@code null}.
044     * @param data The data to store in the cache, may be {@code null}.
045     */
046    void put(RepositorySystemSession session, Object key, Object data);
047
048    /**
049     * Gets the specified data from the cache.
050     * <p>
051     * <em>Warning:</em> The cache will directly return the saved reference. If the cached data is to be modified after
052     * its retrieval, the caller is responsible to create a copy of the returned data and use this instead of the cache
053     * record.
054     *
055     * @param session The repository session during which the cache is accessed, must not be {@code null}.
056     * @param key The key to use for lookup of the data, must not be {@code null}.
057     * @return The requested data or {@code null} if none was present in the cache.
058     */
059    Object get(RepositorySystemSession session, Object key);
060
061    /**
062     * Retrieve or compute the data associated with the specified key.
063     *
064     * @param key The key for which to retrieve the session data, must not be {@code null}.
065     * @param supplier The supplier will compute the new value, must not be {@code null}.
066     * @return The cache data associated with the key.
067     * @since 2.0.0
068     */
069    Object computeIfAbsent(RepositorySystemSession session, Object key, Supplier<Object> supplier);
070}