📄 markupcache.java
字号:
{ if (cacheKey != null) { if (markupCache.containsKey(cacheKey) == false) { markupCache.put(cacheKey, markup); } else { // We don't lock the cache while loading a markup. Thus it may // happen that the very same markup gets loaded twice (the first // markup being loaded, but not yet in the cache, and another // request requesting the very same markup). Since markup // loading in avg takes less than 100ms, it is not really an // issue. For consistency reasons however, we should always use // the markup loaded first which is why it gets returned. markup = (Markup)markupCache.get(cacheKey); } } return markup; } /** * Wicket's default implementation just uses the cacheKey to retrieve the markup from the cache. * More sophisticated implementations may call a container method to e.g. ignore the cached * markup under certain situations. * * @param cacheKey * If null, than the cache will be ignored * @param container * @return null, if not found or to enforce reloading the markup */ protected Markup getMarkupFromCache(final CharSequence cacheKey, final MarkupContainer container) { if (cacheKey != null) { return (Markup)markupCache.get(cacheKey); } return null; } /** * Loads markup from a resource stream. * * @param container * The original requesting markup container * @param markupResourceStream * The markup resource stream to load * @param enforceReload * The cache will be ignored and all, including inherited markup files, will be * reloaded. Whatever is in the cache, it will be ignored * @return The markup */ private final Markup loadMarkup(final MarkupContainer container, final MarkupResourceStream markupResourceStream, final boolean enforceReload) { String cacheKey = markupResourceStream.getCacheKey(); try { Markup markup = getMarkupLoader().loadMarkup(container, markupResourceStream, null, enforceReload); // add the markup to the cache. return putIntoCache(cacheKey, markup); } catch (ResourceStreamNotFoundException e) { log.error("Unable to find markup from " + markupResourceStream, e); } catch (IOException e) { log.error("Unable to read markup from " + markupResourceStream, e); } // In case of an error, remove the cache entry if (cacheKey != null) { removeMarkup(cacheKey); } return Markup.NO_MARKUP; } /** * Load markup from an IResourceStream and add an {@link IChangeListener}to the * {@link ModificationWatcher} so that if the resource changes, we can remove it from the cache * automatically and subsequently reload when needed. * * @param container * The original requesting markup container * @param markupResourceStream * The markup stream to load and begin to watch * @param enforceReload * The cache will be ignored and all, including inherited markup files, will be * reloaded. Whatever is in the cache, it will be ignored * @return The markup in the stream */ private final Markup loadMarkupAndWatchForChanges(final MarkupContainer container, final MarkupResourceStream markupResourceStream, final boolean enforceReload) { final String cacheKey = markupResourceStream.getCacheKey(); if (cacheKey != null) { // Watch file in the future final ModificationWatcher watcher = Application.get() .getResourceSettings() .getResourceWatcher(true); if (watcher != null) { watcher.add(markupResourceStream, new IChangeListener() { public void onChange() { if (log.isDebugEnabled()) { log.debug("Remove markup from cache: " + markupResourceStream); } // Remove the markup from the cache. It will be reloaded // next time when the markup is requested. watcher.remove(markupResourceStream); removeMarkup(cacheKey); } }); } } if (log.isDebugEnabled()) { log.debug("Loading markup from " + markupResourceStream); } return loadMarkup(container, markupResourceStream, enforceReload); } /** * Get the markup cache key provider to be used * * @param container * The MarkupContainer requesting the markup resource stream * @return IMarkupResourceStreamProvider */ public IMarkupCacheKeyProvider getMarkupCacheKeyProvider(final MarkupContainer container) { if (container instanceof IMarkupCacheKeyProvider) { return (IMarkupCacheKeyProvider)container; } if (markupCacheKeyProvider == null) { markupCacheKeyProvider = new DefaultMarkupCacheKeyProvider(); } return markupCacheKeyProvider; } /** * Get the markup resource stream provider to be used * * @param container * The MarkupContainer requesting the markup resource stream * @return IMarkupResourceStreamProvider */ protected IMarkupResourceStreamProvider getMarkupResourceStreamProvider( final MarkupContainer container) { if (container instanceof IMarkupResourceStreamProvider) { return (IMarkupResourceStreamProvider)container; } if (markupResourceStreamProvider == null) { markupResourceStreamProvider = new DefaultMarkupResourceStreamProvider(); } return markupResourceStreamProvider; } /** * In case there is a need to extend the default chain of MarkupLoaders * * @return MarkupLoader */ protected IMarkupLoader getMarkupLoader() { if (markupLoader == null) { markupLoader = new DefaultMarkupLoader(); } return markupLoader; } /** * Allows you to change the map implementation which will hold the cache data. By default it is * a ConcurrentHashMap() in order to allow multiple thread to access the data in a secure way. * * @return */ protected ICache newCacheImplementation() { return new DefaultCacheImplementation(); } /** * MarkupCache allows you to implement you own cache implementation. ICache is the interface the * implementation must comply with. * * @see MarkupCache */ public interface ICache { /** * Clear the cache */ void clear(); /** * Remove an entry from the cache. * * @param key * @return true, if found and removed */ boolean remove(Object key); /** * Get the cache element associated with the key * * @param key * @return */ Object get(Object key); /** * Get all the keys referencing cache entries * * @return */ Collection getKeys(); /** * Check if key is in the cache * * @param key * @return */ boolean containsKey(Object key); /** * Get the number of cache entries * * @return */ int size(); /** * Put an entry into the cache * * @param key * The reference key to find the element * @param value * The element to be cached */ void put(Object key, Object value); /** * Cleanup and shutdown */ void shutdown(); } /** * */ public class DefaultCacheImplementation implements ICache { private static final long serialVersionUID = 1L; private final ConcurrentHashMap cache = new ConcurrentHashMap(); /** * Construct. */ public DefaultCacheImplementation() { } /** * @see org.apache.wicket.markup.MarkupCache.ICache#clear() */ public void clear() { cache.clear(); } /** * @see org.apache.wicket.markup.MarkupCache.ICache#containsKey(java.lang.Object) */ public boolean containsKey(Object key) { return cache.containsKey(key); } /** * @see org.apache.wicket.markup.MarkupCache.ICache#get(java.lang.Object) */ public Object get(Object key) { return cache.get(key); } /** * @see org.apache.wicket.markup.MarkupCache.ICache#getKeys() */ public Collection getKeys() { return cache.keySet(); } /** * @see org.apache.wicket.markup.MarkupCache.ICache#put(java.lang.Object, java.lang.Object) */ public void put(Object key, Object value) { cache.put(key, value); } /** * @see org.apache.wicket.markup.MarkupCache.ICache#remove(java.lang.Object) */ public boolean remove(Object key) { return cache.remove(key) == null; } /** * @see org.apache.wicket.markup.MarkupCache.ICache#size() */ public int size() { return cache.size(); } /** * @see org.apache.wicket.markup.MarkupCache.ICache#shutdown() */ public void shutdown() { clear(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -