Example usage for com.liferay.portal.kernel.cache PortalCache put

List of usage examples for com.liferay.portal.kernel.cache PortalCache put

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.cache PortalCache put.

Prototype

@Proxy
    public void put(K key, V value, int timeToLive);

Source Link

Usage

From source file:br.com.thiagomoreira.liferay.plugins.MarkdownDisplayPortlet.java

License:Apache License

@Override
public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {

    PortletPreferences preferences = request.getPreferences();
    String markdownURL = preferences.getValue("markdownURL", null);
    int timeToLive = 60 * GetterUtil.getInteger(preferences.getValue("timeToLive", "1"));
    boolean autolinks = GetterUtil.getBoolean(preferences.getValue("autolinks", "false"));
    boolean fencedBlockCodes = GetterUtil.getBoolean(preferences.getValue("fencedBlockCodes", "false"));
    boolean tables = GetterUtil.getBoolean(preferences.getValue("tables", "false"));

    if (Validator.isNotNull(markdownURL)) {
        PortalCache<Serializable, Object> portalCache = SingleVMPoolUtil
                .getCache(MarkdownDisplayPortlet.class.getName());

        String content = (String) portalCache.get(markdownURL);

        if (content == null) {
            int options = Extensions.NONE;
            if (autolinks) {
                options = options | Extensions.AUTOLINKS;
            }//from   w w  w . ja va  2 s.  co  m
            if (fencedBlockCodes) {
                options = options | Extensions.FENCED_CODE_BLOCKS;
            }
            if (tables) {
                options = options | Extensions.TABLES;
            }

            PegDownProcessor processor = new PegDownProcessor(options);

            String markdownSource = HttpUtil.URLtoString(markdownURL);
            content = processor.markdownToHtml(markdownSource);

            portalCache.put(markdownURL, content, timeToLive);
        }

        request.setAttribute("content", content);
    } else {
        request.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.TRUE);
    }

    super.render(request, response);
}