001package org.apache.commons.jcs3.access.behavior;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Map;
023import java.util.Set;
024import java.util.function.Supplier;
025
026import org.apache.commons.jcs3.access.exception.CacheException;
027import org.apache.commons.jcs3.engine.behavior.ICacheElement;
028import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
029
030/**
031 * ICacheAccess defines the behavior for client access.
032 */
033public interface ICacheAccess<K, V>
034    extends ICacheAccessManagement
035{
036    /**
037     * Basic get method.
038     * <p>
039     * @param name
040     * @return Object or null if not found.
041     */
042    V get(K name);
043
044    /**
045     * Basic get method. If the object cannot be found in the cache, it will be
046     * retrieved by calling the supplier and subsequently storing it in the cache.
047     * <p>
048     * @param name
049     * @param supplier supplier to be called if the value is not found
050     * @return Object.
051     */
052    V get(K name, Supplier<V> supplier);
053
054    /**
055     * Retrieve matching objects from the cache region this instance provides access to.
056     * <p>
057     * @param pattern - a key pattern for the objects stored
058     * @return A map of key to values. These are stripped from the wrapper.
059     */
060    Map<K, V> getMatching(String pattern);
061
062    /**
063     * Puts in cache if an item does not exist with the name in that region.
064     * <p>
065     * @param name
066     * @param obj
067     * @throws CacheException
068     */
069    void putSafe(K name, V obj)
070        throws CacheException;
071
072    /**
073     * Puts and/or overrides an element with the name in that region.
074     * <p>
075     * @param name
076     * @param obj
077     * @throws CacheException
078     */
079    void put(K name, V obj)
080        throws CacheException;
081
082    /**
083     * Description of the Method
084     * <p>
085     * @param name
086     * @param obj
087     * @param attr
088     * @throws CacheException
089     */
090    void put(K name, V obj, IElementAttributes attr)
091        throws CacheException;
092
093    /**
094     * This method returns the ICacheElement&lt;K, V&gt; wrapper which provides access to element info and other
095     * attributes.
096     * <p>
097     * This returns a reference to the wrapper. Any modifications will be reflected in the cache. No
098     * defensive copy is made.
099     * <p>
100     * This method is most useful if you want to determine things such as the how long the element
101     * has been in the cache.
102     * <p>
103     * The last access time in the ElementAttributes should be current.
104     * <p>
105     * @param name Key the object is stored as
106     * @return The ICacheElement&lt;K, V&gt; if the object is found or null
107     */
108    ICacheElement<K, V> getCacheElement(K name);
109
110    /**
111     * Get multiple elements from the cache based on a set of cache keys.
112     * <p>
113     * This method returns the ICacheElement&lt;K, V&gt; wrapper which provides access to element info and other
114     * attributes.
115     * <p>
116     * This returns a reference to the wrapper. Any modifications will be reflected in the cache. No
117     * defensive copy is made.
118     * <p>
119     * This method is most useful if you want to determine things such as the how long the element
120     * has been in the cache.
121     * <p>
122     * The last access time in the ElementAttributes should be current.
123     * <p>
124     * @param names set of Object cache keys
125     * @return a map of Object key to ICacheElement&lt;K, V&gt; element, or empty map if none of the keys are
126     *         present
127     */
128    Map<K, ICacheElement<K, V>> getCacheElements(Set<K> names);
129
130    /**
131     * Get multiple elements from the cache based on a set of cache keys.
132     * <p>
133     * This method returns the ICacheElement&lt;K, V&gt; wrapper which provides access to element info and other
134     * attributes.
135     * <p>
136     * This returns a reference to the wrapper. Any modifications will be reflected in the cache. No
137     * defensive copy is made.
138     * <p>
139     * This method is most useful if you want to determine things such as the how long the element
140     * has been in the cache.
141     * <p>
142     * The last access time in the ElementAttributes should be current.
143     * <p>
144     * @param pattern key search pattern
145     * @return a map of Object key to ICacheElement&lt;K, V&gt; element, or empty map if no keys match the
146     *         pattern
147     */
148    Map<K, ICacheElement<K, V>> getMatchingCacheElements(String pattern);
149
150    /**
151     * Remove an object for this key if one exists, else do nothing.
152     * <p>
153     * @param name
154     * @throws CacheException
155     */
156    void remove(K name)
157        throws CacheException;
158
159    /**
160     * Reset the attributes on the object matching this key name.
161     * <p>
162     * @param name
163     * @param attributes
164     * @throws CacheException
165     */
166    void resetElementAttributes(K name, IElementAttributes attributes)
167        throws CacheException;
168
169    /**
170     * Gets the elementAttributes attribute of the ICacheAccess object
171     * <p>
172     * @param name
173     * @return The elementAttributes value
174     * @throws CacheException
175     */
176    IElementAttributes getElementAttributes(K name)
177        throws CacheException;
178}