Package br.com.caelum.vraptor.actioncache.tag

Source Code of br.com.caelum.vraptor.actioncache.tag.FragmentCache

package br.com.caelum.vraptor.actioncache.tag;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;

import javax.enterprise.inject.spi.CDI;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import br.com.caelum.vraptor.actioncache.ActionCache;
import br.com.caelum.vraptor.actioncache.ActionCacheEntry;
import br.com.caelum.vraptor.actioncache.CacheKey;

/**
* It should keep jsp fragment in cache.
*
* @author Alberto Souza(@asouza)
*
*/
public class FragmentCache extends SimpleTagSupport

  private int duration;
  private String key; 
  private static ActionCache actionCache = CDI.current().select(ActionCache.class).get();

  @Override
  public void doTag() throws JspException, IOException {
    super.doTag();
    final StringWriter body = new StringWriter();
    CacheKey cacheKey = new CacheKey(key);
    ActionCacheEntry entry = actionCache.fetch(cacheKey);
    if (entry == null) {
      getJspBody().invoke(body);
      actionCache.write(cacheKey, new ActionCacheEntry(body.toString(), new HashMap<String, String>()));
    }
    getJspContext().getOut().println(actionCache.fetch(cacheKey).getResult());
  }

  public void setDuration(int duration) {
    this.duration = duration;
  }

  public void setKey(String key) {
    this.key = key;
  }
}
TOP

Related Classes of br.com.caelum.vraptor.actioncache.tag.FragmentCache

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.