// Copyright 2016 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.exec.apple; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; import javax.annotation.Nullable; /** * General cache file manager for mapping one or more keys to host-related path information. * *
Cache management has some notable restrictions: *
Note that a single cache manager instance is not thread-safe, though multiple threads may
* hold cache manager instances for the same cache file. As a result, it is possible multiple
* threads may write the same entry to cache. This is fine, as retrieval from the cache will simply
* return the first found entry.
*/
final class CacheManager {
private final Path cacheFilePath;
private boolean cacheFileTouched;
/**
* @param outputRoot path to the bazel's output root
* @param cacheFilename name of the cache file
*/
CacheManager(Path outputRoot, String cacheFilename) {
cacheFilePath = outputRoot.getRelative(cacheFilename);
}
private void touchCacheFile() throws IOException {
if (!cacheFileTouched) {
FileSystemUtils.touchFile(cacheFilePath);
cacheFileTouched = true;
}
}
/**
* Returns the value associated with the given list of string keys from the cache,
* or null if the entry is not present in the cache. If there is more than one value for the
* given key, the first value is returned.
*/
@Nullable
public String getValue(String... keys) throws IOException {
Preconditions.checkArgument(keys.length > 0);
touchCacheFile();
List