org.datanucleus.cache.Level2Cache Maven / Gradle / Ivy
Show all versions of datanucleus-core Show documentation
/**********************************************************************
Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
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.
Contributors:
...
**********************************************************************/
package org.datanucleus.cache;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Interface for any Level 2 Cache used internally.
* Provides the typical controls required internally and including the JDO/JPA/Jakarta L2 cache methods.
* JDO/JPA/Jakarta allow the use of a level 2 (L2) cache, with the cache shared between PersistenceManagers/EntityManagers.
* The objects in the level 2 cache don't pertain to any one manager.
*
* The L2 cache stores an object of type org.datanucleus.cache.CachedPC and is keyed by the identity of the object.
* The CachedPC contains the values of fields of a persistable object, together with the indicators for which fields are loaded.
* The relation field values do not store actual objects; they store the identities of the related objects.
* For example if an object X has a 1-1 relation with another persistable object Y then in the relation field values for X for that field
* we store the identity of Y. Similarly if the field is a Collection, then the relation field values will be a Collection of identities of the related objects.
* This provides isolation of each object in the L2 cache (so objects aren't storing references to other objects and so allowing garbage collection etc).
*
*
* Objects are stored in the L2 cache in the following situations
*
*
* - An object is retrieved (from the datastore) within a transaction, and it is stored in the L2 cache if no object with that identity already exists there.
* - At commit() of the transaction any object that has been modified during that transaction will be stored/updated in the L2 cache if its persistable object is
* still in memory in the PM/EM (could have been garbage collected since flushing)
*
*
* Each class can be configured to be cacheable or not.
* The default for a persistable class is to be cacheable. Configuration is performed via annotations or XML metadata.
* If a class is not cacheable then objects of that type aren't stored in the L2 cache.
*
*/
public interface Level2Cache extends Serializable
{
/**
* Method to close the cache when no longer needed. Provides a hook to release resources etc.
*/
void close();
/**
* Evict the parameter instance from the second-level cache.
* @param oid the object id of the instance to evict.
*/
void evict(Object oid);
/**
* Evict the parameter instances from the second-level cache.
* All instances in the PersistenceManager's cache are evicted
* from the second-level cache.
*/
void evictAll();
/**
* Evict the parameter instances from the second-level cache.
* @param oids the object ids of the instance to evict.
*/
void evictAll(Object[] oids);
/**
* Evict the parameter instances from the second-level cache.
* @param oids the object ids of the instance to evict.
*/
void evictAll(Collection oids);
/**
* Evict the parameter instances from the second-level cache.
* @param pcClass the class of instances to evict
* @param subclasses if true, evict instances of subclasses also
*/
void evictAll(Class pcClass, boolean subclasses);
/**
* Accessor for the total number of objects in the L2 cache.
* @return Number of objects
*/
default int getSize()
{
// Some don't support it, so provide a default
return 0;
}
/**
* Accessor for an object from the cache.
* @param oid The Object ID
* @return The L2 cacheable object
*/
CachedPC get(Object oid);
/**
* Accessor for a collection of objects from the cache.
* @param oids The Object IDs
* @return Map of the objects, keyed by the oids that are found
*/
default Map