本文整理汇总了Java中org.ofbiz.base.util.cache.UtilCache类的典型用法代码示例。如果您正苦于以下问题:Java UtilCache类的具体用法?Java UtilCache怎么用?Java UtilCache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UtilCache类属于org.ofbiz.base.util.cache包,在下文中一共展示了UtilCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getStringTemplateInvokerForContent
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
/**
* Gets a (String)TemplateInvoker with compiled Freemarker template for the attribute
* content inline template.
* <p>
* TODO: decide cache... can make CmsAttributeTemplate-specific cache (by id or so, faster) but
* may get memory duplication with #interpretStd calls (cached by body, because no id/name avail)
* -> could do both at once maybe? share the Template instance between 2 cache...
* TODO: subject to refactor/move
* TODO?: FUTURE: this does NOT wrap the TemplateInvoker in a TemplateModel for time being...
* we will rely on the default ObjectWrapper(s) to wrap the StringTemplateInvoker
* in a freemarker StringModel (BeanModel) which renders throughs the ?string built-in
* or string evaluation.
*/
public static TemplateInvoker getStringTemplateInvokerForContent(String tmplStr, Map<String, Object> ctxVars, CmsPageContext pageContext) throws TemplateException, IOException {
Configuration config = CmsRenderTemplate.TemplateRenderer.getDefaultCmsConfig();
// TODO: REVIEW: cache selection is a problem...
// instead of by template body we could cache by some derivative unique name
// derived from the CmsAttributeTemplate ID (maybe?)
// would likely be much faster
UtilCache<String, Template> cache = TemplateSource.getTemplateInlineSelfCacheForConfig(config, null);
if (cache == null) {
Debug.logWarning("Cms: could not determine"
+ " an inline template cache to use; not using cache", module);
}
TemplateSource templateSource = TemplateSource.getForInlineSelfCache(tmplStr, cache, config);
// NOTE: must get StringInvoker so BeansWrapper's StringModel can invoke toString()
// NOTE: context parameters could be passed to the template using InvokeOptions.ctxVars...
// but not yet needed
TemplateInvoker invoker = TemplateInvoker.getInvoker(templateSource, new InvokeOptions(null, null, null, ctxVars, false), null);
return invoker;
}
示例2: put
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
public GenericValue put(GenericPK pk, GenericValue entity) {
if (pk.getModelEntity().getNeverCache()) {
if (Debug.verboseOn()) { // SCIPIO: only log if verbose on (but still log as warning!)
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);
}
return null;
}
if (entity == null) {
entity = GenericValue.NULL_VALUE;
} else {
// before going into the cache, make this value immutable
entity.setImmutable();
}
UtilCache<GenericPK, GenericValue> entityCache = getOrCreateCache(pk.getEntityName());
return entityCache.put(pk, entity);
}
示例3: remove
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
public GenericValue remove(GenericPK pk) {
UtilCache<GenericPK, GenericValue> entityCache = getCache(pk.getEntityName());
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);
if (entityCache == null) return null;
GenericValue retVal = entityCache.remove(pk);
ModelEntity model = pk.getModelEntity();
if (model != null) {
Iterator<String> it = model.getViewConvertorsIterator();
while (it.hasNext()) {
String targetEntityName = it.next();
UtilCache.clearCache(getCacheName(targetEntityName));
}
}
if (Debug.verboseOn()) Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], found this in the cache: " + retVal, module);
return retVal;
}
示例4: getTemplate
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
/**
* Gets a Template instance from the template cache. If the Template instance isn't
* found in the cache, then one will be created.
* <p>
* SCIPIO: 2017-02-21: May now pass cache null to bypass caching.
*/
public static Template getTemplate(String templateLocation, UtilCache<String, Template> cache, Configuration config) throws TemplateException, IOException {
Template template = (cache != null) ? cache.get(templateLocation) : null;
if (template == null) {
// only make the reader if we need it, and then close it right after!
Reader templateReader = makeReader(templateLocation);
try {
template = new Template(templateLocation, templateReader, config);
} finally { // SCIPIO: added finally
templateReader.close();
}
if (cache != null) {
template = cache.putIfAbsentAndGet(templateLocation, template);
}
}
return template;
}
示例5: getTemplateFromString
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
/**
* SCIPIO: Gets template from string out of custom cache (new).
* 2017-02-21: May now pass cache null to bypass caching.
*/
public static Template getTemplateFromString(String templateString, String templateKey, String templateName, UtilCache<String, Template> cache, Configuration config) throws TemplateException, IOException {
Template template = (cache != null) ? cache.get(templateKey) : null;
if (template == null) {
Reader templateReader = new StringReader(templateString);
try {
template = new Template(templateName, templateReader, config);
} finally {
templateReader.close();
}
if (cache != null) {
template = cache.putIfAbsentAndGet(templateKey, template);
}
}
return template;
}
示例6: assertUtilCacheSettings
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
private static <K, V> void assertUtilCacheSettings(UtilCache<K, V> cache, Integer sizeLimit, Integer maxInMemory, Long expireTime, Boolean useSoftReference, Boolean useFileSystemStore) {
if (sizeLimit != null) {
assertEquals(cache.getName() + ":sizeLimit", sizeLimit.intValue(), cache.getSizeLimit());
}
if (maxInMemory != null) {
assertEquals(cache.getName() + ":maxInMemory", maxInMemory.intValue(), cache.getMaxInMemory());
}
if (expireTime != null) {
assertEquals(cache.getName() + ":expireTime", expireTime.longValue(), cache.getExpireTime());
}
if (useSoftReference != null) {
assertEquals(cache.getName() + ":useSoftReference", useSoftReference.booleanValue(), cache.getUseSoftReference());
}
if (useFileSystemStore != null) {
assertEquals(cache.getName() + ":useFileSystemStore", useFileSystemStore.booleanValue(), cache.getUseFileSystemStore());
}
assertEquals("initial empty", true, cache.isEmpty());
assertEquals("empty keys", Collections.emptySet(), cache.getCacheLineKeys());
assertEquals("empty values", Collections.emptyList(), cache.values());
assertSame("find cache", cache, UtilCache.findCache(cache.getName()));
assertNotSame("new cache", cache, UtilCache.createUtilCache());
}
示例7: assertKey
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
public static <K, V> void assertKey(String label, UtilCache<K, V> cache, K key, V value, V other, int size, Map<K, V> map) {
assertNull(label + ":get-empty", cache.get(key));
assertFalse(label + ":containsKey-empty", cache.containsKey(key));
V oldValue = cache.put(key, other);
assertTrue(label + ":containsKey-class", cache.containsKey(key));
assertEquals(label + ":get-class", other, cache.get(key));
assertNull(label + ":oldValue-class", oldValue);
assertEquals(label + ":size-class", size, cache.size());
oldValue = cache.put(key, value);
assertTrue(label + ":containsKey-value", cache.containsKey(key));
assertEquals(label + ":get-value", value, cache.get(key));
assertEquals(label + ":oldValue-value", other, oldValue);
assertEquals(label + ":size-value", size, cache.size());
map.put(key, value);
assertEquals(label + ":map-keys", map.keySet(), cache.getCacheLineKeys());
assertEquals(label + ":map-values", map.values(), cache.values());
}
示例8: testPutIfAbsentAndGet
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
public void testPutIfAbsentAndGet() throws Exception {
UtilCache<String, String> cache = createUtilCache(5, 5, 2000, false, false);
Listener<String, String> gotListener = createListener(cache);
Listener<String, String> wantedListener = new Listener<String, String>();
wantedListener.noteKeyAddition(cache, "key", "value");
wantedListener.noteKeyAddition(cache, "anotherKey", "anotherValue");
assertNull("no-get", cache.get("key"));
assertEquals("putIfAbsentAndGet", "value", cache.putIfAbsentAndGet("key", "value"));
assertHasSingleKey(cache, "key", "value");
assertEquals("putIfAbsentAndGet", "value", cache.putIfAbsentAndGet("key", "newValue"));
assertHasSingleKey(cache, "key", "value");
String anotherValueAddedToCache = new String("anotherValue");
String anotherValueNotAddedToCache = new String("anotherValue");
assertEquals(anotherValueAddedToCache, anotherValueNotAddedToCache);
assertNotSame(anotherValueAddedToCache, anotherValueNotAddedToCache);
String cachedValue = cache.putIfAbsentAndGet("anotherKey", anotherValueAddedToCache);
assertSame(cachedValue, anotherValueAddedToCache);
cachedValue = cache.putIfAbsentAndGet("anotherKey", anotherValueNotAddedToCache);
assertNotSame(cachedValue, anotherValueNotAddedToCache);
assertSame(cachedValue, anotherValueAddedToCache);
cache.removeListener(gotListener);
assertEquals("listener", wantedListener, gotListener);
}
示例9: getTemplateLocationCacheForConfig
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
/**
* Tries to get appropriate location-based UtilCache in use for templates being rendered.
* <p>
* FIXME: WARNING: massive limitations here, we do not have access to all the Configuration instances
* we'd need to test to implement this, and a host of problems...
* as a result I am creating a new two-layer cache system that first keys on the Configuration instances.
*/
public static UtilCache<String, Template> getTemplateLocationCacheForConfig(Configuration config, Environment env) throws TemplateModelException {
Map<Configuration, UtilCache<String, Template>> configTmplLocCaches = TemplateSource.configTmplLocCaches;
UtilCache<String, Template> cache = configTmplLocCaches.get(config);
if (cache == null) {
// double-locking idiom, with configTmplLocCaches as unmodifiable
synchronized(TemplateSource.class) {
configTmplLocCaches = TemplateSource.configTmplLocCaches;
cache = configTmplLocCaches.get(config);
if (cache == null) {
Map<Configuration, UtilCache<String, Template>> newConfigCacheMap = new HashMap<>(configTmplLocCaches);
cache = UtilCache.createUtilCache("templatesource.ftl.location." + (configTmplLocCaches.size() + 1),
0, configTmplCacheExpireTime, false);
newConfigCacheMap.put(config, cache);
TemplateSource.configTmplLocCaches = newConfigCacheMap;
}
}
}
return cache;
}
示例10: getTemplateInlineSelfCacheForConfig
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
/**
* Heuristically tries to get the inline template UtilCache in use for templates being rendered.
* Cache has the templates themselves as keys.
* FIXME: serious limitations and memory implications
* @see #getTemplateLocationCacheForConfig
*/
public static UtilCache<String, Template> getTemplateInlineSelfCacheForConfig(Configuration config, Environment env) throws TemplateModelException {
Map<Configuration, UtilCache<String, Template>> configTmplInlineSelfCaches = TemplateSource.configTmplInlineSelfCaches;
UtilCache<String, Template> cache = configTmplInlineSelfCaches.get(config);
if (cache == null) {
// double-locking idiom, with configTmplInlineSelfCaches as unmodifiable
synchronized(TemplateSource.class) {
configTmplInlineSelfCaches = TemplateSource.configTmplInlineSelfCaches;
cache = configTmplInlineSelfCaches.get(config);
if (cache == null) {
Map<Configuration, UtilCache<String, Template>> newConfigCacheMap = new HashMap<>(configTmplInlineSelfCaches);
cache = UtilCache.createUtilCache("templatesource.ftl.inline.self." + (configTmplInlineSelfCaches.size() + 1),
0, configTmplCacheExpireTime, false);
newConfigCacheMap.put(config, cache);
TemplateSource.configTmplInlineSelfCaches = newConfigCacheMap;
}
}
}
return cache;
}
示例11: clearAllEvent
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
/** An HTTP WebEvent handler that clears all caches
* @param request The HTTP request object for the current JSP or Servlet request.
* @param response The HTTP response object for the current JSP or Servlet request.
* @return return an HTTP WebEvent handler that clears all caches
*/
public static String clearAllEvent(HttpServletRequest request, HttpServletResponse response) {
String errMsg = "";
Locale locale = UtilHttp.getLocale(request);
Security security = (Security) request.getAttribute("security");
if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
errMsg = UtilProperties.getMessage(UtilCacheEvents.err_resource, "utilCacheEvents.permissionEdit", locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
UtilCache.clearAllCaches();
errMsg = UtilProperties.getMessage(UtilCacheEvents.err_resource, "utilCache.clearAllCaches", locale);
request.setAttribute("_EVENT_MESSAGE_", errMsg + " (" + UtilDateTime.nowDateString("yyyy-MM-dd HH:mm:ss") + ").");
return "success";
}
示例12: clearFileCaches
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
/**
* Clears the system cache for location
*
* @param dctx The DispatchContext that this service is operating in
* @param context Map containing the input parameters
* @return Map with the result of the service, the output parameters
*/
public static Map<String, Object> clearFileCaches(DispatchContext dctx, Map<String, ?> context) {
try {
if(UtilMisc.booleanValue(UtilProperties.getPropertyValue("cache", "cache.fileupdate.enable","true"), true)){
String cacheName = (String) context.get("cacheName");
String fileType = (String) context.get("fileType");
String fileLocation = (String) context.get("fileLocation");
UtilCache.clearCache(cacheName);
return ServiceUtil.returnSuccess("Cache cleared for "+cacheName);
}else{
return ServiceUtil.returnSuccess("Cache-Lock set. Cache not cleared");
}
}
catch(Exception e) {
final String errorMsg = "Exception triggering File Event";
Debug.logError(e, errorMsg, module);
return ServiceUtil.returnError(errorMsg + ": " + e.getMessage());
}
}
示例13: clearContentAssocViewCache
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
public static Map<String, Object> clearContentAssocViewCache(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException{
Map<String, Object> results = FastMap.newInstance();
UtilCache<?, ?> utilCache = UtilCache.findCache("entitycache.entity-list.default.ContentAssocViewFrom");
if (utilCache != null) {
utilCache.clear();
}
utilCache = UtilCache.findCache("entitycache.entity-list.default.ContentAssocViewTo");
if (utilCache != null) {
utilCache.clear();
}
return results;
}
示例14: clearContentAssocDataResourceViewCache
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
public static Map<String, Object> clearContentAssocDataResourceViewCache(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException{
Map<String, Object> results = FastMap.newInstance();
UtilCache<?, ?> utilCache = UtilCache.findCache("entitycache.entity-list.default.ContentAssocViewDataResourceFrom");
if (utilCache != null) {
utilCache.clear();
}
utilCache = UtilCache.findCache("entitycache.entity-list.default.ContentAssocViewDataResourceTo");
if (utilCache != null) {
utilCache.clear();
}
return results;
}
示例15: getGenericGlobalCache
import org.ofbiz.base.util.cache.UtilCache; //导入依赖的package包/类
public static <K, V> UtilCache<K, V> getGenericGlobalCache(String cacheName) {
if (CmsObject.ALLOW_GLOBAL_OBJ_CACHE) {
return createCache(cacheName, readExpireTime(cacheName));
} else {
return null;
}
}