Search in sources :

Example 1 with CacheEntry

use of grails.util.CacheEntry in project grails-core by grails.

the class GroovyPageViewResolver method loadView.

@Override
protected View loadView(String viewName, Locale locale) throws Exception {
    Assert.notNull(templateEngine, "Property [templateEngine] cannot be null");
    if (viewName.endsWith(GSP_SUFFIX)) {
        viewName = viewName.substring(0, viewName.length() - GSP_SUFFIX.length());
    }
    if (!allowGrailsViewCaching) {
        return createGrailsView(viewName);
    }
    String viewCacheKey = groovyPageLocator.resolveViewFormat(viewName);
    String currentControllerKeyPrefix = resolveCurrentControllerKeyPrefixes();
    if (currentControllerKeyPrefix != null) {
        viewCacheKey = currentControllerKeyPrefix + ':' + viewCacheKey;
    }
    CacheEntry<View> entry = viewCache.get(viewCacheKey);
    final String lookupViewName = viewName;
    Callable<View> updater = new Callable<View>() {

        public View call() throws Exception {
            try {
                return createGrailsView(lookupViewName);
            } catch (Exception e) {
                throw new WrappedInitializationException(e);
            }
        }
    };
    View view = null;
    if (entry == null) {
        try {
            return CacheEntry.getValue(viewCache, viewCacheKey, cacheTimeout, updater);
        } catch (CacheEntry.UpdateException e) {
            e.rethrowCause();
            // make compiler happy
            return null;
        }
    }
    try {
        view = entry.getValue(cacheTimeout, updater, true, null);
    } catch (WrappedInitializationException e) {
        e.rethrowCause();
    }
    return view;
}
Also used : CacheEntry(grails.util.CacheEntry) View(org.springframework.web.servlet.View) AbstractUrlBasedView(org.springframework.web.servlet.view.AbstractUrlBasedView) Callable(java.util.concurrent.Callable)

Example 2 with CacheEntry

use of grails.util.CacheEntry in project grails-core by grails.

the class GroovyPagesTemplateRenderer method findAndCacheTemplate.

private Template findAndCacheTemplate(Object controller, TemplateVariableBinding pageScope, String templateName, String contextPath, String pluginName, final String uri) throws IOException {
    String templatePath = GrailsStringUtils.isNotEmpty(contextPath) ? GrailsResourceUtils.appendPiecesForUri(contextPath, templateName) : templateName;
    final GroovyPageScriptSource scriptSource;
    if (pluginName == null) {
        scriptSource = groovyPageLocator.findTemplateInBinding(controller, templatePath, pageScope);
    } else {
        scriptSource = groovyPageLocator.findTemplateInBinding(controller, pluginName, templatePath, pageScope);
    }
    String cacheKey;
    if (scriptSource == null) {
        cacheKey = contextPath + pluginName + uri;
    } else {
        if (pluginName != null) {
            cacheKey = contextPath + pluginName + scriptSource.getURI();
        } else {
            cacheKey = scriptSource.getURI();
        }
    }
    return CacheEntry.getValue(templateCache, cacheKey, reloadEnabled ? GroovyPageMetaInfo.LASTMODIFIED_CHECK_INTERVAL : -1, null, new Callable<CacheEntry<Template>>() {

        public CacheEntry<Template> call() {
            return new CacheEntry<Template>() {

                boolean allowCaching = cacheEnabled;

                boolean neverExpire = false;

                @Override
                protected boolean hasExpired(long timeout, Object cacheRequestObject) {
                    return neverExpire ? false : (allowCaching ? super.hasExpired(timeout, cacheRequestObject) : true);
                }

                @Override
                public boolean isInitialized() {
                    return allowCaching ? super.isInitialized() : false;
                }

                @Override
                public void setValue(Template val) {
                    if (allowCaching) {
                        super.setValue(val);
                    }
                }

                @Override
                protected Template updateValue(Template oldValue, Callable<Template> updater, Object cacheRequestObject) throws Exception {
                    Template t = null;
                    if (scriptSource != null) {
                        t = groovyPagesTemplateEngine.createTemplate(scriptSource);
                    }
                    if (t == null && scaffoldingTemplateGenerator != null) {
                        t = generateScaffoldedTemplate(GrailsWebRequest.lookup(), uri);
                        // always enable caching for generated
                        // scaffolded template
                        allowCaching = true;
                        // never expire scaffolded entry since scaffolding plugin flushes the whole cache on any change
                        neverExpire = true;
                    }
                    return t;
                }
            };
        }
    }, true, null);
}
Also used : GroovyPageScriptSource(org.grails.gsp.io.GroovyPageScriptSource) GroovyObject(groovy.lang.GroovyObject) CacheEntry(grails.util.CacheEntry) GrailsTagException(org.grails.taglib.GrailsTagException) IOException(java.io.IOException) Template(groovy.text.Template)

Aggregations

CacheEntry (grails.util.CacheEntry)2 GroovyObject (groovy.lang.GroovyObject)1 Template (groovy.text.Template)1 IOException (java.io.IOException)1 Callable (java.util.concurrent.Callable)1 GroovyPageScriptSource (org.grails.gsp.io.GroovyPageScriptSource)1 GrailsTagException (org.grails.taglib.GrailsTagException)1 View (org.springframework.web.servlet.View)1 AbstractUrlBasedView (org.springframework.web.servlet.view.AbstractUrlBasedView)1