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

io.gravitee.node.api.cache.Cache Maven / Gradle / Ivy

There is a newer version: 7.0.0-alpha.13
Show newest version
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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.gravitee.node.api.cache;

import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
public interface Cache {
    /**
     * Get the name of this cache.
     *
     * @return the name if this cache.
     */
    String getName();

    /**
     * Get a collection of all the values in this cache.
     *
     * @return a collection of all the values in the cache.
     */
    Collection values();

    /**
     * Reactive method to get all the values in this cache as a Flowable.
     *
     * @return a Flowable containing all the values in the cache.
     */
    default Flowable rxValues() {
        return Flowable.fromIterable(this.values()).subscribeOn(Schedulers.io());
    }

    /**
     * Get a set of all the keys in this cache.
     *
     * @return a set of all the keys in the cache.
     */
    Set keys();

    /**
     * Reactive method to get all the keys in this cache as a Flowable.
     *
     * @return a Flowable containing all the keys in the cache.
     */
    default Flowable rxKeys() {
        return Flowable.fromIterable(this.keys()).subscribeOn(Schedulers.io());
    }

    /**
     * Get set of all the entries in the cache.
     *
     * @return all the entries in the cache.
     */
    Set> entrySet();

    /**
     * Reactive method to get all the entries in this cache as a Flowable.
     *
     * @return a Flowable containing all the entries in the cache.
     */
    default Flowable> rxEntrySet() {
        return Flowable.fromIterable(this.entrySet()).subscribeOn(Schedulers.io());
    }

    /**
     * Get the current size of this cache.
     *
     * @return the size of the cache.
     */
    int size();

    /**
     * Reactive method to get the current size of this cache.
     *
     * @return a Single containing the size of the cache.
     */
    default Single rxSize() {
        return Single.fromCallable(this::size).subscribeOn(Schedulers.io());
    }

    /**
     * Check if this cache is empty.
     *
     * @return true if the cache is empty, false otherwise.
     */
    boolean isEmpty();

    /**
     * Reactive method to check if this cache is empty.
     *
     * @return a Single containing true if the cache is empty, false otherwise.
     */
    default Single rxIsEmpty() {
        return Single.fromCallable(this::isEmpty).subscribeOn(Schedulers.io());
    }

    /**
     * Check if the specified key is in this cache.
     * @param key the key.
     *
     * @return true if the key is in the cache, false otherwise.
     */
    boolean containsKey(final K key);

    /**
     * Reactive method to check if the specified key is in this cache.
     * @param key the key.
     *
     * @return a Single containing true if the key is in the cache, false otherwise.
     */
    default Single rxContainsKey(final K key) {
        return Single.fromCallable(() -> this.containsKey(key)).subscribeOn(Schedulers.io());
    }

    /**
     * Return the value for the specified key or null if this cache does not contain this key.
     * @param key the key used to get the value.
     *
     * @return the value or null if no value has been found.
     */
    V get(final K key);

    /**
     * Reactive method to get the value for the specified key as a Maybe. An empty Maybe is returned if this cache does not contain this key.
     * @param key the key used to get the value.
     *
     * @return a Maybe containing the value or an empty Maybe if no value has been found.
     */
    default Maybe rxGet(final K key) {
        return Maybe.fromCallable(() -> this.get(key)).subscribeOn(Schedulers.io());
    }

    /**
     * Associate the value to the specified key in this cache and returns the previous value that was associated to this key or null
     * if no value was previously associated to this key.
     * @param key the key.
     * @param value the value.
     *
     * @return the previous value associated to the specified key or null if no value was present.
     */
    V put(final K key, final V value);

    /**
     * Reactive method to associate the value to the specified key in this cache and return a Maybe containing the previous value that was associated to this key.
     * An empty Maybe is returned if no value was previously associated to this key.
     * @param key the key.
     * @param value the value.
     *
     * @return a Maybe containing the previous value or an empty Maybe if no value was present.
     */
    default Maybe rxPut(final K key, final V value) {
        return Maybe.fromCallable(() -> this.put(key, value)).subscribeOn(Schedulers.io());
    }

    /**
     * Same as {@link #put(Object, Object)} but with a time to live.
     * @param key the key.
     * @param value the value.
     *
     * @return the previous value associated to the specified key or null if no value was present.
     */
    V put(final K key, final V value, final long ttl, final TimeUnit ttlUnit);

    /**
     * Same as {@link #rxPut(Object, Object)} but with a time to live.
     * @param key the key.
     * @param value the value.
     *
     * @return the previous value associated to the specified key or null if no value was present.
     */
    default Maybe rxPut(final K key, final V value, final long ttl, final TimeUnit ttlUnit) {
        return Maybe.fromCallable(() -> this.put(key, value, ttl, ttlUnit)).subscribeOn(Schedulers.io());
    }

    /**
     * Put all the specified key/value pairs into this cache.
     * @param m a map containing all the key/value pairs to put into this cache.
     */
    void putAll(final Map m);

    /**
     * Reactive method to put all the specified key/value pairs into this cache.
     * @param m a map containing all the key/value pairs to put into this cache.
     *
     * @return a Completable that completes once all the key/value pairs have been put in the cache.
     */
    default Completable rxPutAll(final Map m) {
        return Completable.fromRunnable(() -> this.putAll(m)).subscribeOn(Schedulers.io());
    }

    /**
     * If the specified key is not already associated with a value, attempt to compute the value of the specified key using the given remapping function.
     * If the remapping function returns null, the mapping is removed.
     * @param key the key.
     * @param remappingFunction the function that is executed to associate the new value in case no value was present.
     *
     * @return the computed value if it was absent (null if the mapping function returns null) or the existing one if it was already present.
     */
    V computeIfAbsent(K key, Function remappingFunction);

    /**
     * Reactive method to compute the value of the specified key using the given mapping function if it is not already associated with a value.
     * If the remapping function returns null, the mapping is removed.
     * @param key the key.
     * @param mappingFunction the function that is executed to associate the new value in case no value was present.
     *
     * @return a Maybe containing the computed value if it was absent (empty if the mapping function returns null) or the existing one if it was already present.
     */
    default Maybe rxComputeIfAbsent(K key, Function mappingFunction) {
        return Maybe.fromCallable(() -> this.computeIfAbsent(key, mappingFunction)).subscribeOn(Schedulers.io());
    }

    /**
     * If the specified key is already associated with a value, attempt to compute the value of the specified key using the given mapping function.
     * If the remapping function returns null, the mapping is removed.
     * @param key the key.
     * @param remappingFunction the function that is executed to associate the new value in case a value was present.
     *
     * @return the computed value (null if the mapping function returns null).
     */
    V computeIfPresent(K key, BiFunction remappingFunction);

    /**
     * Reactive method to compute the value of the specified key using the given mapping function if it is already associated with a value.
     * If the remapping function returns null, the mapping is removed.
     * @param key the key.
     * @param remappingFunction the function that is executed to associate the new value in case a value was present.
     *
     * @return a Maybe containing the computed value (empty if the mapping function returns null).
     */
    default Maybe rxComputeIfPresent(K key, BiFunction remappingFunction) {
        return Maybe.fromCallable(() -> this.computeIfPresent(key, remappingFunction)).subscribeOn(Schedulers.io());
    }

    /**
     * Compute the value of the specified key using the given mapping function.
     * If the remapping function returns null, the mapping is removed.
     * @param key the key.
     * @param remappingFunction the function that is executed to associate the new value.
     *
     * @return the computed value (null if the mapping function returns null).
     */
    V compute(K key, BiFunction remappingFunction);

    /**
     * Reactive method to compute the value of the specified key using the given mapping function.
     * @param key the key.
     * @param remappingFunction the function that is executed to associate the new value.
     *
     * @return a Maybe containing the computed value (empty one if the mapping function returns null).
     */
    default Maybe rxCompute(K key, BiFunction remappingFunction) {
        return Maybe.fromCallable(() -> this.compute(key, remappingFunction)).subscribeOn(Schedulers.io());
    }

    /**
     * Remove the key/value entry from the cache.
     * @param key the key.
     *
     * @return the remove value or null if it wasn't present.
     */
    V evict(final K key);

    /**
     * Reactive method to remove the key/value entry from the cache.
     * @param key the key.
     *
     * @return a Maybe containing the removed value or an empty one if it wasn't present.
     */
    default Maybe rxEvict(final K key) {
        return Maybe.fromCallable(() -> this.evict(key)).subscribeOn(Schedulers.io());
    }

    /**
     * Clear the cache.
     */
    void clear();

    /**
     * Reactive method to clear the cache.
     *
     * @return a Completable that completes when the cache has been cleared.
     */
    default Completable rxClear() {
        return Completable.fromRunnable(this::clear).subscribeOn(Schedulers.io());
    }

    /**
     * Add a listener that will be called on for each action on this cache.
     * @param listener the cache listener.
     *
     * @return an uuid for this cache listener that can be used to remove this it.
     */
    String addCacheListener(CacheListener listener);

    /**
     * Reactive method to add a listener that will be called on for each action on this cache.
     * @param listener the cache listener.
     *
     * @return a Single containing an uuid for this cache listener that can be used to remove this it.
     */
    default Single rxAddCacheListener(CacheListener listener) {
        return Single.fromCallable(() -> this.addCacheListener(listener)).subscribeOn(Schedulers.io());
    }

    /**
     * Remove the specified cache listener.
     * @param listenerCacheId the id of the cache listener to remove.
     *
     * @return true if the cache listener has been removed, false otherwise.
     */
    boolean removeCacheListener(final String listenerCacheId);

    /**
     * Reactive method to remove the specified cache listener.
     * @param listenerCacheId the id of the cache listener to remove.
     *
     * @return a Single containing true if the cache listener has been remove or false otherwise.
     */
    default Single rxRemoveCacheListener(final String listenerCacheId) {
        return Single.fromCallable(() -> this.removeCacheListener(listenerCacheId)).subscribeOn(Schedulers.io());
    }
}