1 25 package org.ofbiz.entity.cache; 26 27 import org.ofbiz.base.util.cache.UtilCache; 28 import org.ofbiz.entity.GenericDelegator; 29 30 public abstract class AbstractCache { 31 32 protected String delegatorName, id; 33 34 protected AbstractCache(String delegatorName, String id) { 35 this.delegatorName = delegatorName; 36 this.id = id; 37 } 38 39 public GenericDelegator getDelegator() { 40 return GenericDelegator.getGenericDelegator(delegatorName); 41 } 42 43 public void remove(String entityName) { 44 UtilCache.clearCache(getCacheName(entityName)); 45 } 46 47 public void clear() { 48 UtilCache.clearCachesThatStartWith(getCacheNamePrefix()); 49 } 50 51 public String getCacheNamePrefix() { 52 return "entitycache." + id + "." + delegatorName + "."; 53 } 54 55 public String [] getCacheNamePrefixes() { 56 return new String [] { 57 "entitycache." + id + ".${delegator-name}.", 58 "entitycache." + id + "." + delegatorName + "." 59 }; 60 } 61 62 public String getCacheName(String entityName) { 63 return getCacheNamePrefix() + entityName; 64 } 65 66 public String [] getCacheNames(String entityName) { 67 String [] prefixes = getCacheNamePrefixes(); 68 String [] names = new String [prefixes.length * 2]; 69 for (int i = 0; i < prefixes.length; i++) { 70 names[i] = prefixes[i] + "${entity-name}"; 71 } 72 for (int i = prefixes.length, j = 0; j < prefixes.length; i++, j++) { 73 names[i] = prefixes[j] + entityName; 74 } 75 return names; 76 } 77 78 protected UtilCache getCache(String entityName) { 79 synchronized (UtilCache.utilCacheTable) { 80 return (UtilCache) UtilCache.utilCacheTable.get(getCacheName(entityName)); 81 } 82 } 83 84 protected UtilCache getOrCreateCache(String entityName) { 85 synchronized (UtilCache.utilCacheTable) { 86 String name = getCacheName(entityName); 87 UtilCache cache = (UtilCache) UtilCache.utilCacheTable.get(name); 88 if (cache == null) { 89 cache = new UtilCache(name, 0, 0, true); 90 String [] names = getCacheNames(entityName); 91 cache.setPropertiesParams(names); 92 } 93 return cache; 94 } 95 } 96 } 97 | Popular Tags |