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

io.micronaut.cache.DelegatingAsyncCache Maven / Gradle / Ivy

/*
 * Copyright 2017-2019 original 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 io.micronaut.cache;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.type.Argument;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

/**
 * An asynchronous cache that delegates blocking cache operations
 * to the provided executor.
 *
 * @param  The cache type
 * @author James Kleeh
 * @since 1.3.0
 */
@Internal
public class DelegatingAsyncCache implements AsyncCache {

    private final SyncCache delegate;
    private final ExecutorService executorService;

    /**
     * @param delegate The delegate blocking cache
     * @param executorService The executor service to run the blocking operations on
     */
    public DelegatingAsyncCache(SyncCache delegate, ExecutorService executorService) {
        this.delegate = delegate;
        this.executorService = executorService;
    }

    @Override
    public  CompletableFuture> get(Object key, Argument requiredType) {
        return CompletableFuture.supplyAsync(() -> delegate.get(key, requiredType), executorService);
    }

    @Override
    public  CompletableFuture get(Object key, Argument requiredType, Supplier supplier) {
        return CompletableFuture.supplyAsync(() -> delegate.get(key, requiredType, supplier), executorService);
    }

    @Override
    public  CompletableFuture> putIfAbsent(Object key, T value) {
        return CompletableFuture.supplyAsync(() -> delegate.putIfAbsent(key, value), executorService);
    }

    @Override
    public String getName() {
        return delegate.getName();
    }

    @Override
    public C getNativeCache() {
        return delegate.getNativeCache();
    }

    @Override
    public CompletableFuture put(Object key, Object value) {
        return CompletableFuture.supplyAsync(() -> {
            delegate.put(key, value);
            return true;
        }, executorService);
    }

    @Override
    public CompletableFuture invalidate(Object key) {
        return CompletableFuture.supplyAsync(() -> {
            delegate.invalidate(key);
            return true;
        }, executorService);
    }

    @Override
    public CompletableFuture invalidateAll() {
        return CompletableFuture.supplyAsync(() -> {
            delegate.invalidateAll();
            return true;
        }, executorService);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy