org.gradle.testfixtures.internal.InMemoryCacheFactory 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 2011 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.testfixtures.internal;
import com.google.common.collect.Maps;
import org.gradle.api.Action;
import org.gradle.cache.*;
import org.gradle.cache.internal.CacheFactory;
import org.gradle.cache.internal.filelock.LockOptions;
import org.gradle.internal.Factory;
import org.gradle.internal.serialize.Serializer;
import org.gradle.util.GFileUtils;
import java.io.File;
import java.util.Collections;
import java.util.Map;
public class InMemoryCacheFactory implements CacheFactory {
public PersistentCache openStore(File storeDir, String displayName, LockOptions lockOptions, Action super PersistentCache> initializer) throws CacheOpenException {
return open(storeDir, displayName, null, Collections.emptyMap(), lockOptions, initializer);
}
public PersistentCache open(File cacheDir, String displayName, CacheValidator cacheValidator, Map properties, LockOptions lockOptions, Action super PersistentCache> initializer) {
GFileUtils.mkdirs(cacheDir);
InMemoryCache cache = new InMemoryCache(cacheDir);
if (initializer != null) {
initializer.execute(cache);
}
return cache;
}
public PersistentIndexedCache openIndexedCache(File cacheDir, CacheValidator validator, Map properties, LockOptions lockOptions, Serializer serializer) {
return new InMemoryIndexedCache(serializer);
}
public static class InMemoryCache implements PersistentCache {
final Map> caches = Maps.newLinkedHashMap();
private final File cacheDir;
private boolean closed;
public InMemoryCache(File cacheDir) {
this.cacheDir = cacheDir;
}
public void close() {
closed = true;
}
public boolean isClosed() {
return closed;
}
public File getBaseDir() {
return cacheDir;
}
private void assertNotClosed() {
if (closed) {
throw new IllegalStateException("cache is closed");
}
}
public PersistentIndexedCache createCache(String name, Class keyType, Serializer valueSerializer) {
assertNotClosed();
return createCache(name, valueSerializer);
}
public PersistentIndexedCache createCache(PersistentIndexedCacheParameters parameters) {
assertNotClosed();
return createCache(parameters.getCacheName(), parameters.getValueSerializer());
}
private PersistentIndexedCache createCache(String name, Serializer valueSerializer) {
assertNotClosed();
InMemoryIndexedCache cache = new InMemoryIndexedCache(valueSerializer);
caches.put(name, cache);
return cache;
}
public T useCache(String operationDisplayName, Factory extends T> action) {
assertNotClosed();
// The contract of useCache() means we have to provide some basic synchronization.
synchronized (this) {
return action.create();
}
}
public void useCache(String operationDisplayName, Runnable action) {
assertNotClosed();
action.run();
}
public T longRunningOperation(String operationDisplayName, Factory extends T> action) {
assertNotClosed();
return action.create();
}
public void longRunningOperation(String operationDisplayName, Runnable action) {
assertNotClosed();
action.run();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy