package org.apache.commons.jcs3.access; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. */ import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; import org.apache.commons.jcs3.access.behavior.ICacheAccess; import org.apache.commons.jcs3.access.exception.CacheException; import org.apache.commons.jcs3.access.exception.InvalidArgumentException; import org.apache.commons.jcs3.access.exception.InvalidHandleException; import org.apache.commons.jcs3.access.exception.ObjectExistsException; import org.apache.commons.jcs3.engine.CacheElement; import org.apache.commons.jcs3.engine.behavior.ICacheElement; import org.apache.commons.jcs3.engine.behavior.IElementAttributes; import org.apache.commons.jcs3.engine.control.CompositeCache; /** * This class provides an interface for all types of access to the cache. *
* An instance of this class is tied to a specific cache region. Static methods are provided to get * such instances. *
* Using this class you can retrieve an item, the item's wrapper, and the element's configuration. You can also put an * item in the cache, remove an item, and clear a region. *
* The JCS class is the preferred way to access these methods.
*/
public class CacheAccess
* @param cacheControl The cache which the created instance accesses
*/
public CacheAccess( final CompositeCache
* @param name Key the object is stored as
* @return The object if found or null
*/
@Override
public V get( final K name )
{
final ICacheElement
* @param name
* @param supplier supplier to be called if the value is not found
* @return Object.
*/
@Override
public V get(final K name, final Supplier
* @param pattern - a key pattern for the objects stored
* @return A map of key to values. These are stripped from the wrapper.
*/
@Override
public Map
* This returns a reference to the wrapper. Any modifications will be reflected in the cache. No
* defensive copy is made.
*
* This method is most useful if you want to determine things such as the how long the element
* has been in the cache.
*
* The last access time in the ElementAttributes should be current.
*
* @param name Key the Serializable is stored as
* @return The ICacheElement<K, V> if the object is found or null
*/
@Override
public ICacheElement
* This method returns the ICacheElement<K, V> wrapper which provides access to element info and other
* attributes.
*
* This returns a reference to the wrapper. Any modifications will be reflected in the cache. No
* defensive copy is made.
*
* This method is most useful if you want to determine things such as the how long the element
* has been in the cache.
*
* The last access time in the ElementAttributes should be current.
*
* @param names set of Serializable cache keys
* @return a map of K key to ICacheElement<K, V> element, or empty map if none of the keys are present
*/
@Override
public Map
* This method returns the ICacheElement<K, V> wrapper which provides access to element info and other
* attributes.
*
* This returns a reference to the wrapper. Any modifications will be reflected in the cache. No
* defensive copy is made.
*
* This method is most useful if you want to determine things such as the how long the element
* has been in the cache.
*
* The last access time in the ElementAttributes should be current.
*
* @param pattern key search pattern
* @return a map of K key to ICacheElement<K, V> element, or empty map if no keys match the pattern
*/
@Override
public Map
* @param key Key object will be stored with
* @param value Object to store
* @throws CacheException and ObjectExistsException is thrown if the item is already in the
* cache.
*/
@Override
public void putSafe( final K key, final V value )
{
if ( this.getCacheControl().get( key ) != null )
{
throw new ObjectExistsException( "putSafe failed. Object exists in the cache for key [" + key
+ "]. Remove first or use a non-safe put to override the value." );
}
put( key, value );
}
/**
* Place a new object in the cache, associated with key name. If there is currently an object
* associated with name in the region it is replaced. Names are scoped to a region so they must
* be unique within the region they are placed.
* @param name Key object will be stored with
* @param obj Object to store
*/
@Override
public void put( final K name, final V obj )
{
// Call put with a copy of the contained caches default attributes.
// the attributes are copied by the cacheControl
put( name, obj, this.getCacheControl().getElementAttributes() );
}
/**
* Constructs a cache element with these attributes, and puts it into the cache.
*
* If the key or the value is null, and InvalidArgumentException is thrown.
*
* @see org.apache.commons.jcs3.access.behavior.ICacheAccess#put(Object, Object, IElementAttributes)
*/
@Override
public void put( final K key, final V val, final IElementAttributes attr )
{
if ( key == null )
{
throw new InvalidArgumentException( "Key must not be null" );
}
if ( val == null )
{
throw new InvalidArgumentException( "Value must not be null" );
}
// Create the element and update. This may throw an IOException which
// should be wrapped by cache access.
try
{
final CacheElement
* @param name the name of the item to remove.
*/
@Override
public void remove( final K name )
{
this.getCacheControl().remove( name );
}
/**
* Reset attributes for a particular element in the cache. NOTE: this method is currently not
* implemented.
*
* @param name Key of object to reset attributes for
* @param attr New attributes for the object
* @throws InvalidHandleException if the item does not exist.
*/
@Override
public void resetElementAttributes( final K name, final IElementAttributes attr )
{
final ICacheElement
* @param name Key of object to get attributes for
* @return Attributes for the object, null if object not in cache
*/
@Override
public IElementAttributes getElementAttributes( final K name ) throws CacheException
{
IElementAttributes attr = null;
try
{
attr = this.getCacheControl().getElementAttributes( name );
}
catch ( final IOException ioe )
{
throw new CacheException("Failure getting element attributes", ioe);
}
return attr;
}
}