All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.testfixtures.internal.InMemoryCacheFactory Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * 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 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 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 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 action) {
            assertNotClosed();
            return action.create();
        }

        public void longRunningOperation(String operationDisplayName, Runnable action) {
            assertNotClosed();
            action.run();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy