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

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

There is a newer version: 4.6.5
Show newest version
/*
 * Copyright 2017-2020 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.function.Supplier;

/**
 * Transforms a synchronous cache into one that meets the asynchronous
 * contract while still running operations on the same thread.
 *
 * @param  The cache type
 * @author James Kleeh
 * @since 1.3.0
 */
@Internal
public class DelegatingAsyncBlockingCache implements AsyncCache {

    private final SyncCache delegate;

    /**
     * @param delegate The delegate blocking cache
     */
    public DelegatingAsyncBlockingCache(SyncCache delegate) {
        this.delegate = delegate;
    }
    
    @Override
    public  CompletableFuture> get(Object key, Argument requiredType) {
        try {
            return CompletableFuture.completedFuture(delegate.get(key, requiredType));
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @Override
    public  CompletableFuture get(Object key, Argument requiredType, Supplier supplier) {
        try {
            return CompletableFuture.completedFuture(delegate.get(key, requiredType, supplier));
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @Override
    public  CompletableFuture> putIfAbsent(Object key, T value) {
        try {
            return CompletableFuture.completedFuture(delegate.putIfAbsent(key, value));
        } catch (Exception e) {
            return handleException(e);
        }
    }

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

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

    @Override
    public CompletableFuture put(Object key, Object value) {
        try {
            delegate.put(key, value);
            return CompletableFuture.completedFuture(true);
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @Override
    public CompletableFuture invalidate(Object key) {
        try {
            delegate.invalidate(key);
            return CompletableFuture.completedFuture(true);
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @Override
    public CompletableFuture invalidateAll() {
        try {
            delegate.invalidateAll();
            return CompletableFuture.completedFuture(true);
        } catch (Exception e) {
            return handleException(e);
        }
    }

    private  CompletableFuture handleException(Exception e) {
        CompletableFuture future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy