/** * Runs the given block in the context of a cache. * If you do not run your block this way, the caching * mechanism will be disabled. * * @param block block */ public static void runBlock(InternableBlock block) { String processId = getProcessId(); cachedBlocks.add(processId); block.run(); cachedBlocks.remove(processId); clearAll(processId); }
/** * Returns an internalized value if it already exists in the cache * * @param value value * * @return the internalized value */ public static Object intern(Object key, Object value) { String processId = getProcessId(); if (!cachedBlocks.contains(processId)) { return value; } synchronized (cachedValues) { List values = (List) cachedValues.get(processId, key); if (values == null) { cachedValues.put(processId, key, (values = new ArrayList())); } Object intern; if (values.contains(value)) { intern = values.get(values.indexOf(value)); } else { values.add(intern = value); } return intern; } }