/** * INTERNAL: * return true if this object is expire, false otherwise. */ public abstract boolean isInvalidated(CacheKey key, long currentTimeMillis);
/** * INTERNAL: * return true if this object is expire, false otherwise. */ public abstract boolean isInvalidated(CacheKey key, long currentTimeMillis);
/** * INTERNAL: * return true if this object is expire, false otherwise. */ public abstract boolean isInvalidated(CacheKey key, long currentTimeMillis);
/** * ADVANCED: * Using a list of Entity PK this method will attempt to bulk load the entire list from the cache. * In certain circumstances this can have large performance improvements over loading each item individually. * @param pkList List of Entity PKs to extract from the cache * @param ClassDescriptor Descriptor type to be retrieved. * @return Map of Entity PKs associated to the Entities that were retrieved * @throws QueryException */ public Map<Object, CacheKey> getAllCacheKeysFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session){ HashMap<Object, CacheKey> map = new HashMap<Object, CacheKey>(); CacheKey cachedObject = null; long currentTime = System.currentTimeMillis(); for (Object pk : pkList){ cachedObject = getCacheKey(pk, false); if ((cachedObject != null && cachedObject.getObject() != null && !descriptor.getCacheInvalidationPolicy().isInvalidated(cachedObject, currentTime))){ map.put(pk, cachedObject); } } return map; }
/** * ADVANCED: * Using a list of Entity PK this method will attempt to bulk load the entire list from the cache. * In certain circumstances this can have large performance improvements over loading each item individually. * @param pkList List of Entity PKs to extract from the cache * @param ClassDescriptor Descriptor type to be retrieved. * @return Map of Entity PKs associated to the Entities that were retrieved * @throws QueryException */ public Map<Object, CacheKey> getAllCacheKeysFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session){ HashMap<Object, CacheKey> map = new HashMap<Object, CacheKey>(); CacheKey cachedObject = null; long currentTime = System.currentTimeMillis(); for (Object pk : pkList){ cachedObject = getCacheKey(pk, false); if ((cachedObject != null && cachedObject.getObject() != null && !descriptor.getCacheInvalidationPolicy().isInvalidated(cachedObject, currentTime))){ map.put(pk, cachedObject); } } return map; }
/** * ADVANCED: * Using a list of Entity PK this method will attempt to bulk load the entire list from the cache. * In certain circumstances this can have large performance improvements over loading each item individually. * @param pkList List of Entity PKs to extract from the cache * @param ClassDescriptor Descriptor type to be retrieved. * @return Map of Entity PKs associated to the Entities that were retrieved * @throws QueryException */ public Map<Object, Object> getAllFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session){ HashMap<Object, Object> map = new HashMap<Object, Object>(); CacheKey cachedObject = null; long currentTime = System.currentTimeMillis(); for (Object pk : pkList){ cachedObject = getCacheKey(pk, false); if ((cachedObject != null && cachedObject.getObject() != null && !descriptor.getCacheInvalidationPolicy().isInvalidated(cachedObject, currentTime))){ map.put(pk, cachedObject.getObject()); } } return map; }
/** * INTERNAL: * Check if the object is invalid and *should* be refreshed. * This is used to ensure that no invalid objects are cloned. */ public boolean isConsideredInvalid(Object object, CacheKey cacheKey, ClassDescriptor descriptor) { if (cacheKey.getObject() != null) { CacheInvalidationPolicy cachePolicy = descriptor.getCacheInvalidationPolicy(); // BUG#6671556 refresh invalid objects when accessed in the unit of work. return (cachePolicy.shouldRefreshInvalidObjectsOnClone() && cachePolicy.isInvalidated(cacheKey)); } return false; }
/** * INTERNAL: * Check if the object is invalid and *should* be refreshed. * This is used to ensure that no invalid objects are cloned. */ public boolean isConsideredInvalid(Object object, CacheKey cacheKey, ClassDescriptor descriptor) { if (cacheKey.getObject() != null) { CacheInvalidationPolicy cachePolicy = descriptor.getCacheInvalidationPolicy(); // BUG#6671556 refresh invalid objects when accessed in the unit of work. return (cachePolicy.shouldRefreshInvalidObjectsOnClone() && cachePolicy.isInvalidated(cacheKey)); } return false; }
/** * ADVANCED: * Using a list of Entity PK this method will attempt to bulk load the entire list from the cache. * In certain circumstances this can have large performance improvements over loading each item individually. * @param pkList List of Entity PKs to extract from the cache * @param ClassDescriptor Descriptor type to be retrieved. * @return Map of Entity PKs associated to the Entities that were retrieved * @throws QueryException */ public Map<Object, Object> getAllFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session){ HashMap<Object, Object> map = new HashMap<Object, Object>(); CacheKey cachedObject = null; long currentTime = System.currentTimeMillis(); for (Object pk : pkList){ cachedObject = getCacheKey(pk, false); if ((cachedObject != null && cachedObject.getObject() != null && !descriptor.getCacheInvalidationPolicy().isInvalidated(cachedObject, currentTime))){ map.put(pk, cachedObject.getObject()); } } return map; }
/** * Return the cache key for the cache index or null if not found. */ public CacheKey getCacheKeyByIndex(CacheIndex index, CacheId indexValues, boolean shouldCheckExpiry, ClassDescriptor descriptor) { if (this.cacheIndexes == null) { return null; } IdentityMap map = this.cacheIndexes.get(index); if (map == null) { return null; } CacheKey cacheKey = map.getCacheKey(indexValues, false); if (cacheKey == null) { return null; } // The cache key is nested as the object cache key is put into the cache. cacheKey = (CacheKey)cacheKey.getObject(); if (cacheKey == null) { return null; } if (shouldCheckExpiry && descriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey)) { return null; } return cacheKey; }
/** * Return the cache key for the cache index or null if not found. */ public CacheKey getCacheKeyByIndex(CacheIndex index, CacheId indexValues, boolean shouldCheckExpiry, ClassDescriptor descriptor) { if (this.cacheIndexes == null) { return null; } IdentityMap map = this.cacheIndexes.get(index); if (map == null) { return null; } CacheKey cacheKey = map.getCacheKey(indexValues, false); if (cacheKey == null) { return null; } // The cache key is nested as the object cache key is put into the cache. cacheKey = (CacheKey)cacheKey.getObject(); if (cacheKey == null) { return null; } if (shouldCheckExpiry && descriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey)) { return null; } return cacheKey; }
/** * INTERNAL: * Check if the object is invalid and should be refreshed, return true, otherwise return false. * This is used to ensure that no invalid objects are registered. */ public boolean checkInvalidObject(Object object, CacheKey cacheKey, ClassDescriptor descriptor, UnitOfWorkImpl unitOfWork) { if (!unitOfWork.isNestedUnitOfWork() && (cacheKey.getObject() != null)) { CacheInvalidationPolicy cachePolicy = descriptor.getCacheInvalidationPolicy(); // BUG#6671556 refresh invalid objects when accessed in the unit of work. return (cachePolicy.shouldRefreshInvalidObjectsInUnitOfWork() && cachePolicy.isInvalidated(cacheKey)); } return false; }
/** * Get the cached results associated with a query. Results are cached by the * values of the parameters to the query so different parameters will have * different cached results. */ public Object getQueryResult(ReadQuery query, List parameters, boolean shouldCheckExpiry) { if (query.getQueryResultsCachePolicy() == null) { return null; } // PERF: use query name, unless no name. Object queryKey = query.getName(); if ((queryKey == null) || ((String)queryKey).length() == 0) { queryKey = query; } IdentityMap map = this.queryResults.get(queryKey); if (map == null) { return null; } Object lookupParameters; if ((parameters == null) || parameters.isEmpty()) { lookupParameters = CacheId.EMPTY; } else { lookupParameters = new CacheId(parameters.toArray()); } CacheKey key = map.getCacheKey(lookupParameters, false); if ((key == null) || (shouldCheckExpiry && query.getQueryResultsCachePolicy().getCacheInvalidationPolicy().isInvalidated(key))) { return null; } return key.getObject(); }
/** * ADVANCED: * Return if this object is valid in the cache. */ public boolean isValid(Object primaryKey, Class theClass) { ClassDescriptor descriptor = getSession().getDescriptor(theClass); //forward the call to getCacheKeyForObject locally in case subclasses overload CacheKey key = getCacheKeyForObjectForLock(primaryKey, theClass, descriptor); if (key == null) { throw QueryException.classPkDoesNotExistInCache(theClass, primaryKey); } return !descriptor.getCacheInvalidationPolicy().isInvalidated(key); }
/** * ADVANCED: * Return if this object is valid in the cache. */ public boolean isValid(Object primaryKey, Class theClass) { ClassDescriptor descriptor = getSession().getDescriptor(theClass); //forward the call to getCacheKeyForObject locally in case subclasses overload CacheKey key = getCacheKeyForObjectForLock(primaryKey, theClass, descriptor); if (key == null) { throw QueryException.classPkDoesNotExistInCache(theClass, primaryKey); } return !descriptor.getCacheInvalidationPolicy().isInvalidated(key); }
/** * ADVANCED: * Return if this object is valid in the cache. */ public boolean isValid(Vector primaryKey, Class theClass) { ClassDescriptor descriptor = getSession().getDescriptor(theClass); //forward the call to getCacheKeyForObject locally in case subclasses overload CacheKey key = this.getCacheKeyForObjectForLock(primaryKey, theClass, descriptor); if (key == null) { throw QueryException.classPkDoesNotExistInCache(theClass, primaryKey); } return !descriptor.getCacheInvalidationPolicy().isInvalidated(key); }
/** * Get the cached results associated with a query. Results are cached by the * values of the parameters to the query so different parameters will have * different cached results. */ public Object getQueryResult(ReadQuery query, Vector parameters, boolean shouldCheckExpiry) { if (query.getQueryResultsCachePolicy() == null) { return null; } // PERF: use query name, unless no name. Object queryKey = query.getName(); if ((queryKey == null) || ((String)queryKey).length() == 0) { queryKey = query; } IdentityMap map = this.queryResults.get(queryKey); if (map == null) { return null; } Vector lookupParameters = parameters; if (lookupParameters == null) { lookupParameters = new NonSynchronizedVector(0); } CacheKey key = map.getCacheKey(lookupParameters); if ((key == null) || (shouldCheckExpiry && query.getQueryResultsCachePolicy().getCacheInvalidationPolicy().isInvalidated(key))) { return null; } return key.getObject(); }
/** * INTERNAL: * Check if the object is invalid and refresh it. * This is used to ensure that no invalid objects are registered. */ public void checkInvalidObject(Object object, CacheKey cacheKey, ClassDescriptor descriptor) { if (!isNestedUnitOfWork() && (cacheKey.getObject() != null)) { CacheInvalidationPolicy cachePolicy = descriptor.getCacheInvalidationPolicy(); // BUG#6671556 refresh invalid objects when accessed in the unit of work. if (cachePolicy.shouldRefreshInvalidObjectsInUnitOfWork() && cachePolicy.isInvalidated(cacheKey)) { ReadObjectQuery query = new ReadObjectQuery(); query.setReferenceClass(object.getClass()); query.setSelectionKey(cacheKey.getKey()); query.refreshIdentityMapResult(); query.setIsExecutionClone(true); parent.executeQuery(query); } } }
/** * Returns true if the cache contains an Object with the id and Class type, and is valid. * @see Cache#contains(Class, Object) */ public boolean contains(Class cls, Object id) { getEntityManagerFactory().verifyOpen(); Object pk = createPrimaryKeyFromId(cls, id); if(null == pk) { return false; } ClassDescriptor descriptor = getSession().getClassDescriptor(cls); // getDescriptor() is the same call /** * Check for no descriptor associated with the class parameter. * This will occur if the class represents a MappedSuperclass (concrete or abstract class), * an interface or Embeddable class. */ if(null == descriptor) { // do not throw an IAException: cache_impl_class_has_no_descriptor_is_not_a_persistent_type - just return false return false; } // we can assume that all implementors of IdentityMapAccessor implement getCacheKeyforObject CacheKey key = ((org.eclipse.persistence.internal.sessions.IdentityMapAccessor)getAccessor()) .getCacheKeyForObject(pk, cls, descriptor, false); return key != null && key.getObject() != null && !descriptor.getCacheInvalidationPolicy().isInvalidated(key); }
if ((cacheKey != null) && (shouldReturnInvalidatedObjects || !descriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey))) {