1 25 package org.ofbiz.entity.cache; 26 27 import java.util.Iterator ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.cache.UtilCache; 31 import org.ofbiz.base.util.cache.CacheLine; 32 import org.ofbiz.entity.GenericEntity; 33 import org.ofbiz.entity.GenericPK; 34 import org.ofbiz.entity.condition.EntityCondition; 35 36 public class EntityCache extends AbstractCache { 37 public static final String module = EntityCache.class.getName(); 38 39 public EntityCache(String delegatorName) { 40 super(delegatorName, "entity"); 41 } 42 43 public GenericEntity get(GenericPK pk) { 44 UtilCache entityCache = getCache(pk.getEntityName()); 45 if (entityCache == null) return null; 46 return (GenericEntity) entityCache.get(pk); 47 } 48 49 public GenericEntity put(GenericEntity entity) { 50 if (entity == null) return null; 51 return put(entity.getPrimaryKey(), entity); 52 } 53 54 public GenericEntity put(GenericPK pk, GenericEntity entity) { 55 if (pk.getModelEntity().getNeverCache()) { 56 Debug.logWarning("Tried to put a value of the " + pk.getEntityName() + " entity in the BY PRIMARY KEY cache but this entity has never-cache set to true, not caching.", module); 57 return null; 58 } 59 60 if (entity == null) { 61 entity = GenericEntity.NULL_ENTITY; 62 } else { 63 entity.setImmutable(); 65 } 66 UtilCache entityCache = getOrCreateCache(pk.getEntityName()); 67 return (GenericEntity)entityCache.put(pk, entity); 68 } 69 70 public void remove(String entityName, EntityCondition condition) { 71 UtilCache entityCache = getCache(entityName); 72 if (entityCache == null) return; 73 Iterator it = entityCache.getCacheLineValues().iterator(); 74 while (it.hasNext()) { 75 CacheLine line = (CacheLine) it.next(); 76 if (entityCache.hasExpired(line)) continue; 77 GenericEntity entity = (GenericEntity) line.getValue(); 78 if (entity == null) continue; 79 if (condition.entityMatches(entity)) it.remove(); 80 } 81 } 82 83 public GenericEntity remove(GenericEntity entity) { 84 return remove(entity.getPrimaryKey()); 85 } 86 87 public GenericEntity remove(GenericPK pk) { 88 UtilCache entityCache = getCache(pk.getEntityName()); 89 if (Debug.verboseOn()) Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], will remove from this cache: " + (entityCache == null ? "[No cache found to remove from]" : entityCache.getName()), module); 90 if (entityCache == null) return null; 91 GenericEntity retVal = (GenericEntity) entityCache.remove(pk); 92 if (Debug.verboseOn()) Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], found this in the cache: " + retVal, module); 93 return retVal; 94 } 95 } 96 | Popular Tags |