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; 020 021import java.io.IOException; 022import java.util.HashMap; 023import java.util.Iterator; 024import java.util.List; 025import java.util.Map; 026 027import lucee.commons.io.cache.Cache; 028import lucee.commons.io.cache.CacheEntry; 029import lucee.commons.io.cache.exp.CacheException; 030import lucee.loader.util.Util; 031import lucee.runtime.cache.util.CacheKeyFilterAll; 032import lucee.runtime.cache.util.WildCardFilter; 033import lucee.runtime.op.Caster; 034import lucee.runtime.type.Array; 035import lucee.runtime.type.ArrayImpl; 036import lucee.runtime.type.Struct; 037import lucee.runtime.type.StructImpl; 038import lucee.runtime.type.dt.TimeSpan; 039 040import org.apache.oro.text.regex.MalformedPatternException; 041// MUST this must be come from configuration 042public class CacheEngine { 043 044 045 private static Map caches=new HashMap(); 046 private Cache cache; 047 048 public CacheEngine(Cache cache) { 049 this.cache=cache; 050 } 051 052 053 public void delete(String key,boolean throwWhenNotExists) throws IOException { 054 if(!cache.remove(key) && throwWhenNotExists) 055 throw new CacheException("there is no entry in cache with key ["+key+"]"); 056 } 057 058 public boolean exists(String key) { 059 return cache.contains(key); 060 } 061 062 public int flush(String key, String filter) throws MalformedPatternException, IOException { 063 if(!Util.isEmpty(key)) return cache.remove(key)?1:0; 064 if(!Util.isEmpty(filter)) return cache.remove(new WildCardFilter(filter,false)); 065 return cache.remove(CacheKeyFilterAll.getInstance()); 066 } 067 068 public Object get(String key, Object defaultValue) { 069 return cache.getValue(key, defaultValue); 070 } 071 072 public Object get(String key) throws IOException { 073 return cache.getValue(key); 074 } 075 076 public Array keys(String filter) { 077 try { 078 List keys; 079 if(Util.isEmpty(filter)) keys=cache.keys(); 080 else keys=cache.keys(new WildCardFilter(filter,false)); 081 return Caster.toArray(keys); 082 } 083 catch (Exception e) {} 084 return new ArrayImpl(); 085 } 086 087 public Struct list(String filter) { 088 089 Struct sct=new StructImpl(); 090 try { 091 List entries; 092 if(Util.isEmpty(filter)) entries=cache.entries(); 093 else entries=cache.entries(new WildCardFilter(filter,false)); 094 095 Iterator it = entries.iterator(); 096 CacheEntry entry; 097 while(it.hasNext()){ 098 entry=(CacheEntry) it.next(); 099 sct.setEL(entry.getKey(), entry.getValue()); 100 } 101 } 102 catch (Exception e) {e.printStackTrace();} 103 return sct; 104 } 105 106 public void set(String key, Object value, TimeSpan timespan) { 107 Long until=timespan==null?null:Long.valueOf(timespan.getMillis()); 108 cache.put(key, value, null, until); 109 } 110 111 public Struct info() { 112 return cache.getCustomInfo(); 113 } 114 115 public Struct info(String key) throws IOException { 116 if(key==null) return info(); 117 CacheEntry entry = cache.getCacheEntry(key); 118 return entry.getCustomInfo(); 119 } 120 121 public Cache getCache() { 122 return cache; 123 } 124 125 126 127 128 129 /*public static void updateConnection(Config config, String name, String className, Struct connection, boolean _default) throws IOException { 130 Document doc = getDocument(config); 131 132 Element parent= getChildByName(doc.getDocumentElement(),"connections"); 133 Element[] connections=getChildren(parent,"connection"); 134 Element conn; 135 String str; 136 137 // update 138 boolean updated=false; 139 for(int i=0;i<connections.length;i++){ 140 conn = connections[i]; 141 str=conn.getAttribute("name"); 142 if(_default)conn.setAttribute("default", "false"); 143 if(!Util.isEmpty(str) && str.trim().equalsIgnoreCase(name.trim())){ 144 updateConnection(conn,name,className,connection,_default); 145 updated=true; 146 } 147 } 148 149 // insert 150 if(!updated){ 151 conn=doc.createElement("connection"); 152 updateConnection(conn,name,className,connection,_default); 153 parent.appendChild(conn); 154 } 155 store(config,doc); 156 } 157 158 private static void updateConnection(Element conn, String name, String className, Struct custom, boolean _default) { 159 conn.setAttribute("name", name); 160 conn.setAttribute("class", className); 161 conn.setAttribute("custom", toString(custom)); 162 conn.setAttribute("default", _default?"true":"false"); 163 } 164 165 public static void deleteConnection(Config config, String name) throws IOException { 166 Document doc = getDocument(config); 167 168 Element parent= getChildByName(doc.getDocumentElement(),"connections"); 169 Element[] connections=getChildren(parent,"connection"); 170 Element conn; 171 String str; 172 for(int i=0;i<connections.length;i++){ 173 conn = connections[i]; 174 str=conn.getAttribute("name"); 175 if(Util.isEmpty(str) || !str.trim().equalsIgnoreCase(name.trim())) 176 continue; 177 parent.removeChild(conn); 178 } 179 store(config,doc); 180 }*/ 181 182 183 184 185 186 187 188 189}