001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Maxime Hilaire
018 *
019 */
020package org.nuxeo.ecm.core.cache;
021
022import java.io.Serializable;
023import java.util.Set;
024
025/**
026 * The nuxeo cache interface that define generic methods to use cache technologies
027 *
028 * @since 6.0
029 */
030public interface Cache {
031
032    /**
033     * Get cache name as specified in the descriptor
034     *
035     * @return the cache name
036     * @since 6.0
037     */
038    String getName();
039
040    /**
041     * Get method to retrieve value from cache Must not raise exception if the key is null, but return null
042     *
043     * @param key the string key
044     * @return the {@link Serializable} value, return null if the key does not exist or if the key is null
045     * @since 6.0
046     */
047    Serializable get(String key);
048
049    /**
050     * Returns the set of all keys stored in the cache.
051     *
052     * @return the {@link Set} of all keys
053     * @since 8.3
054     */
055    Set<String> keySet();
056
057    /**
058     * Invalidate the given key
059     *
060     * @param key, the key to remove from the cache, if null will do nothing
061     * @since 6.0
062     */
063    void invalidate(String key);
064
065    /**
066     * Invalidate all key-value stored in the cache
067     *
068     * @since 6.0
069     */
070    void invalidateAll();
071
072    /**
073     * Put method to store a {@link Serializable} value
074     *
075     * @param key the string key, if null, the value will not be stored
076     * @param value the value to store, if null, the value will not be stored
077     * @since 6.0
078     */
079    void put(String key, Serializable value);
080
081    /**
082     * Check if a given key is present inside the cache. Compared to the get() method, this method must not update
083     * internal cache state and change TTL
084     *
085     * @param key the string key
086     * @return true if a corresponding entry exists, false otherwise
087     * @since 7.2
088     */
089    default boolean hasEntry(String key) {
090        return get(key) != null;
091    }
092
093}