org.gradle.cache.internal.DefaultCacheFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2010 the original author or authors.
*
* 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 org.gradle.cache.internal;
import org.gradle.api.Action;
import org.gradle.cache.*;
import org.gradle.cache.internal.filelock.LockOptions;
import org.gradle.internal.Factory;
import org.gradle.internal.FileUtils;
import org.gradle.internal.concurrent.CompositeStoppable;
import org.gradle.internal.serialize.Serializer;
import java.io.Closeable;
import java.io.File;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DefaultCacheFactory implements CacheFactory, Closeable {
private final Map dirCaches = new HashMap();
private final FileLockManager lockManager;
private final Lock lock = new ReentrantLock();
public DefaultCacheFactory(FileLockManager fileLockManager) {
this.lockManager = fileLockManager;
}
void onOpen(Object cache) {
}
void onClose(Object cache) {
}
public PersistentCache open(File cacheDir, String displayName, CacheValidator cacheValidator, Map properties, LockOptions lockOptions, Action super PersistentCache> initializer) throws CacheOpenException {
lock.lock();
try {
return doOpen(cacheDir, displayName, cacheValidator, properties, lockOptions, initializer);
} finally {
lock.unlock();
}
}
public PersistentCache openStore(File storeDir, String displayName, LockOptions lockOptions, Action super PersistentCache> initializer) throws CacheOpenException {
lock.lock();
try {
return doOpenStore(storeDir, displayName, lockOptions, initializer);
} finally {
lock.unlock();
}
}
public void close() {
lock.lock();
try {
CompositeStoppable.stoppable(dirCaches.values()).stop();
} finally {
dirCaches.clear();
lock.unlock();
}
}
private PersistentCache doOpen(File cacheDir, String displayName, CacheValidator validator, Map properties, LockOptions lockOptions, Action super PersistentCache> action) {
File canonicalDir = FileUtils.canonicalize(cacheDir);
DirCacheReference dirCacheReference = dirCaches.get(canonicalDir);
if (dirCacheReference == null) {
ReferencablePersistentCache cache = new DefaultPersistentDirectoryCache(canonicalDir, displayName, validator, properties, lockOptions, action, lockManager);
cache.open();
dirCacheReference = new DirCacheReference(cache, properties, lockOptions);
dirCaches.put(canonicalDir, dirCacheReference);
} else {
if (!lockOptions.equals(dirCacheReference.lockOptions)) {
throw new IllegalStateException(String.format("Cache '%s' is already open with different options.", cacheDir));
}
if (!properties.equals(dirCacheReference.properties)) {
throw new IllegalStateException(String.format("Cache '%s' is already open with different state.", cacheDir));
}
}
return new ReferenceTrackingCache(dirCacheReference);
}
private PersistentCache doOpenStore(File storeDir, String displayName, LockOptions lockOptions, Action super PersistentCache> initializer) throws CacheOpenException {
if (initializer != null) {
throw new UnsupportedOperationException("Initializer actions are not currently supported by the directory store implementation.");
}
File canonicalDir = FileUtils.canonicalize(storeDir);
DirCacheReference dirCacheReference = dirCaches.get(canonicalDir);
if (dirCacheReference == null) {
ReferencablePersistentCache cache = new DefaultPersistentDirectoryStore(canonicalDir, displayName, lockOptions, lockManager);
cache.open();
dirCacheReference = new DirCacheReference(cache, Collections.emptyMap(), lockOptions);
dirCaches.put(canonicalDir, dirCacheReference);
}
return new ReferenceTrackingCache(dirCacheReference);
}
private class DirCacheReference implements Closeable {
private final Map properties;
private final LockOptions lockOptions;
private final ReferencablePersistentCache cache;
private final Set references = new HashSet();
public DirCacheReference(ReferencablePersistentCache cache, Map properties, LockOptions lockOptions) {
this.cache = cache;
this.properties = properties;
this.lockOptions = lockOptions;
onOpen(cache);
}
public void addReference(ReferenceTrackingCache cache) {
references.add(cache);
}
public void release(ReferenceTrackingCache cache) {
lock.lock();
try {
if (references.remove(cache) && references.isEmpty()) {
close();
}
} finally {
lock.unlock();
}
}
public void close() {
onClose(cache);
dirCaches.values().remove(this);
references.clear();
cache.close();
}
}
private static class ReferenceTrackingCache implements PersistentCache {
private final DirCacheReference reference;
private ReferenceTrackingCache(DirCacheReference reference) {
this.reference = reference;
reference.addReference(this);
}
@Override
public String toString() {
return reference.cache.toString();
}
public void close() {
reference.release(this);
}
public File getBaseDir() {
return reference.cache.getBaseDir();
}
public PersistentIndexedCache createCache(PersistentIndexedCacheParameters parameters) {
return reference.cache.createCache(parameters);
}
public PersistentIndexedCache createCache(String name, Class keyType, Serializer valueSerializer) {
return reference.cache.createCache(name, keyType, valueSerializer);
}
public T longRunningOperation(String operationDisplayName, Factory extends T> action) {
return reference.cache.longRunningOperation(operationDisplayName, action);
}
public void longRunningOperation(String operationDisplayName, Runnable action) {
reference.cache.longRunningOperation(operationDisplayName, action);
}
public T useCache(String operationDisplayName, Factory extends T> action) {
return reference.cache.useCache(operationDisplayName, action);
}
public void useCache(String operationDisplayName, Runnable action) {
reference.cache.useCache(operationDisplayName, action);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy