001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.cache;
020    
021    import java.util.Collection;
022    import java.util.Set;
023    
024    /**
025     * A Cache efficiently stores temporary objects primarily to improve an application's performance.
026     *
027     * <p>Shiro doesn't implement a full Cache mechanism itself, since that is outside the core competency of a
028     * Security framework.  Instead, this interface provides an abstraction (wrapper) API on top of an underlying
029     * cache framework's cache instance (e.g. JCache, Ehcache, JCS, OSCache, JBossCache, TerraCotta, Coherence,
030     * GigaSpaces, etc, etc), allowing a Shiro user to configure any cache mechanism they choose.
031     *
032     * @author Les Hazlewood
033     * @author Jeremy Haile
034     * @since 0.2
035     */
036    public interface Cache<K, V> {
037    
038        /**
039         * Returns the Cached value stored under the specified {@code key} or
040         * {@code null} if there is no Cache entry for that {@code key}.
041         *
042         * @param key the key that the value was previous added with
043         * @return the cached object or {@code null} if there is no entry for the specified {@code key}
044         * @throws CacheException if there is a problem accessing the underlying cache system
045         */
046        public V get(K key) throws CacheException;
047    
048        /**
049         * Adds a Cache entry.
050         *
051         * @param key   the key used to identify the object being stored.
052         * @param value the value to be stored in the cache.
053         * @return the previous value associated with the given {@code key} or {@code null} if there was previous value
054         * @throws CacheException if there is a problem accessing the underlying cache system
055         */
056        public V put(K key, V value) throws CacheException;
057    
058        /**
059         * Remove the cache entry corresponding to the specified key.
060         *
061         * @param key the key of the entry to be removed.
062         * @return the previous value associated with the given {@code key} or {@code null} if there was previous value
063         * @throws CacheException if there is a problem accessing the underlying cache system
064         */
065        public V remove(K key) throws CacheException;
066    
067        /**
068         * Clear all entries from the cache.
069         *
070         * @throws CacheException if there is a problem accessing the underlying cache system
071         */
072        public void clear() throws CacheException;
073    
074        /**
075         * Returns the number of entries in the cache.
076         *
077         * @return the number of entries in the cache.
078         */
079        public int size();
080    
081        /**
082         * Returns a view of all the keys for entries contained in this cache.
083         *
084         * @return a view of all the keys for entries contained in this cache.
085         */
086        public Set<K> keys();
087    
088        /**
089         * Returns a view of all of the values contained in this cache.
090         *
091         * @return a view of all of the values contained in this cache.
092         */
093        public Collection<V> values();
094    }