001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.cache.legacy; 020 021import java.io.IOException; 022import java.io.OutputStream; 023 024import javax.servlet.http.HttpServletRequest; 025 026import lucee.commons.io.cache.Cache; 027import lucee.commons.io.res.Resource; 028import lucee.commons.lang.StringUtil; 029import lucee.runtime.PageContext; 030import lucee.runtime.config.ConfigImpl; 031import lucee.runtime.functions.cache.Util; 032import lucee.runtime.type.dt.TimeSpan; 033 034import org.apache.oro.text.regex.MalformedPatternException; 035 036 037public abstract class CacheItem { 038 039 protected final String fileName; 040 public static CacheItem getInstance(PageContext pc, String id, String key, boolean useId, Resource dir, String cacheName, TimeSpan timespan) throws IOException{ 041 HttpServletRequest req = pc. getHttpServletRequest(); 042 Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_TEMPLATE,null); 043 if(cache!=null) 044 return new CacheItemCache(pc, req, id, key, useId, cache,timespan); 045 return new CacheItemFS(pc, req, id, key, useId, dir); 046 } 047 048 public CacheItem(PageContext pc, HttpServletRequest req, String id, String key, boolean useId) { 049 050 //raw 051 String filename=req.getServletPath(); 052 if(!StringUtil.isEmpty(req.getQueryString())) { 053 filename+="?"+req.getQueryString(); 054 if(useId)filename+="&cfcache_id="+id; 055 } 056 else { 057 if(useId)filename+="?cfcache_id="+id; 058 } 059 if(useId && !StringUtil.isEmpty(key)) filename=key; 060 if(!StringUtil.isEmpty(req.getContextPath())) filename=req.getContextPath()+filename; 061 fileName=filename; 062 063 064 065 066 } 067 068 069 070 public abstract boolean isValid(); 071 072 public abstract boolean isValid(TimeSpan timespan); 073 074 public abstract void writeTo(OutputStream os, String charset) throws IOException; 075 076 public abstract String getValue() throws IOException; 077 078 public abstract void store(String result) throws IOException; 079 080 public abstract void store(byte[] barr,boolean append) throws IOException; 081 082 //protected abstract void _flushAll(PageContext pc, Resource dir) throws IOException; 083 084 //protected abstract void _flush(PageContext pc, Resource dir, String expireurl) throws IOException; 085 086 public static void flushAll(PageContext pc, Resource dir, String cacheName) throws IOException { 087 Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_TEMPLATE,null); 088 if(cache!=null) CacheItemCache._flushAll(pc, cache); 089 else CacheItemFS._flushAll(pc, dir); 090 } 091 092 public static void flush(PageContext pc, Resource dir, String cacheName,String expireurl) throws IOException, MalformedPatternException { 093 Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_TEMPLATE,null); 094 if(cache!=null) CacheItemCache._flush(pc, cache,expireurl); 095 else CacheItemFS._flush(pc, dir, expireurl); 096 } 097}