001 package railo.runtime.cache.legacy; 002 003 import java.io.ByteArrayInputStream; 004 import java.io.IOException; 005 import java.io.OutputStream; 006 007 import javax.servlet.http.HttpServletRequest; 008 009 import org.apache.oro.text.regex.MalformedPatternException; 010 011 import railo.commons.io.IOUtil; 012 import railo.commons.io.cache.Cache; 013 import railo.commons.lang.ExceptionUtil; 014 import railo.commons.lang.StringUtil; 015 import railo.runtime.PageContext; 016 import railo.runtime.cache.util.CacheKeyFilterAll; 017 import railo.runtime.cache.util.WildCardFilter; 018 import railo.runtime.exp.PageException; 019 import railo.runtime.op.Caster; 020 import railo.runtime.type.dt.TimeSpan; 021 022 public class CacheItemCache extends CacheItem { 023 024 private Cache cache; 025 private TimeSpan timespan; 026 private String lcFileName; 027 028 public CacheItemCache(PageContext pc, HttpServletRequest req, String id, String key, boolean useId, Cache cache, TimeSpan timespan) throws IOException { 029 super(pc, req, id, key, useId); 030 this.cache=cache; 031 this.timespan=timespan; 032 lcFileName=fileName; 033 } 034 035 @Override 036 public boolean isValid() { 037 try { 038 return cache.getValue(lcFileName)!=null; 039 } catch (IOException e) { 040 return false; 041 } 042 } 043 044 @Override 045 public boolean isValid(TimeSpan timespan) { 046 return isValid(); 047 } 048 049 @Override 050 public void writeTo(OutputStream os,String charset) throws IOException { 051 byte[] barr = getValue().getBytes(StringUtil.isEmpty(charset,true)?"UTF-8":charset); 052 IOUtil.copy(new ByteArrayInputStream(barr),os,true,false); 053 } 054 055 public String getValue() throws IOException { 056 try { 057 return Caster.toString(cache.getValue(lcFileName)); 058 } catch (PageException e) { 059 throw ExceptionUtil.toIOException(e); 060 } 061 } 062 063 @Override 064 public void store(String value) throws IOException { 065 cache.put(lcFileName, value, null,valueOf(timespan)); 066 067 } 068 069 @Override 070 public void store(byte[] barr, boolean append) throws IOException { 071 String value=(append)?getValue():""; 072 value+=IOUtil.toString(barr, "UTF-8"); 073 store(value); 074 } 075 076 public static void _flushAll(PageContext pc, Cache cache) { 077 cache.remove(CacheKeyFilterAll.getInstance()); 078 } 079 080 public static void _flush(PageContext pc, Cache cache, String expireurl) throws MalformedPatternException { 081 cache.remove(new WildCardFilter(expireurl,true)); 082 } 083 084 private static Long valueOf(TimeSpan timeSpan) { 085 if(timeSpan==null) return null; 086 return Long.valueOf(timeSpan.getMillis()); 087 } 088 089 }